簡體   English   中英

獲取內容並結合“續行”

[英]Get-Content & combine “continued lines”

我使用Get-Content將txt文件拉入數組,該文件使用_作為行繼續令牌,並且連續行的數量可以是從1到多的任何數字。 所以文字可能看起來像這樣......

Jrn.Directive "DocSymbol"  _
, "[Commercial-Default.rte]"
Jrn.Directive "GlobalToProj"  _
, "[Commercial-Default.rte]", "Floor Plan: Level 1" _
, 0.01041666666667 _
, 1.00000000000000, 0.00000000000000, 0.00000000000000 _
, 0.00000000000000, 1.00000000000000, 0.00000000000000 _
, 0.00000000000000, 0.00000000000000, 1.00000000000000 _
, 0.00000000000000, 0.00000000000000, 0.00000000000000

我想重新格式化沒有換行,我想知道是否有一些超級優雅的方法,我沒有看到? 因為我認為前進的方式是$ array中的foreach $行,如果行EndsWith(“ ”)設置行索引的起始索引,則向前搜索直到行不是EndsWith(“ ”)並且設置結束索引,組合位並寫入臨時數組,然后在主循環繼續讀取行時跳過兩個索引之間的差異。 如果沒有更詳細的偽代碼,這是有道理的。 在任何情況下,它看起來都很笨拙和不優雅,我想知道是否有更好的方法?

我最初的想法是Get-Content可能內置了一些東西,但看起來你可以定義的唯一分隔符是End of Line(默認為\\ n)。

因此,根據Anthony的輸入,並意識到我需要首先組合線,然后刪除不相關的線(可能已經多行開始)我現在有這個。

$target = 'Z:\Support\Px 3.0\RFO Benchmark\Journal Cleanup\journal.0010.txt'
$cleanFile = 'Z:\Support\Px 3.0\RFO Benchmark\Journal Cleanup\journal.0010.CLEAN.txt'

$sourceFile = Get-Content $target

$cleanData = @()


function Relavant {
    [CmdletBinding()]
    param (
        [string]$line
    )

    $irrelevant = @('Jrn.Directive “Username"', 'Jrn.Directive "IdleTimeTaskSymbol"', 'Jrn.Directive "WindowSize"', 'Jrn.Size')

    foreach ($item in $irrelevant) {
        if ($line.StartsWith($item)) {
            $relevant = $false
        } else {
            $relevant = $true
        }
    }

$relevant    
}

$string = ''
$continue = $false
$tempData = $(foreach ($line in $tempData) {
    if ($line -match '^[^,]') {
        $string = ''
        $continue = $true
    }
    if ($continue) {
        $string += $line
    }
    if ($line.EndsWith('_')) {
        $continue = $true
    } else {
        $continue = $false
        $string -replace '\s?_'
    }
})

# Remove comments & irrelevant lines and do basic formatting
foreach ($line in $tempData) {
    $line = $line.Trim()
    if (-not ($line.StartsWith("'"))) {
        if (Relavant $line) {
            $line = $line -replace " ,", ","
            $line = $line -replace '\s+', ' '
            $cleanData += $line
        }
    }
}

Add-Content $cleanFile "' Cleaned by PxJournalCleaner`n"
foreach ($line in $cleanData) {
    Add-Content $cleanFile $line
}

它運作良好,但我懷疑如果沒有別的辦法,我會再次使用替代方法來實施它。 我也不確定我完全理解安東尼的做法是什么,所以我顯然還有一些需要做的事情。 謝謝大家!

你應該讓正則表達式匹配更精確,但它對我有用

$file = gc 'C:\temp\new 1.txt'

$string = ''
$cont = $false
$result = $(foreach ($line in $file) {
    if ($line -match '^[^,]') {
        $string = ''
        $cont = $true
    }
    if ($cont) {
        $string += $line
    }
    if ($line.EndsWith('_')) {
        $cont = $true
    } else {
        $cont = $false
        $string -replace '\s?_'
    }
})

$result

你的方法似乎完全沒問題,雖然我可能只是一次做一行。

你可以這樣做:

# read the wrapped lines from file
$lines = Get-Content C:\yourfile.txt
# initialize an array with a single empty string + a cursor that we'll use to keep track of the last index
$unwrappedLines = ,""
$cursor = 0
# iterate over the input strings
foreach($line in $lines){
    if($line.EndsWith(" _")){
        # Line is to be continued, remove line continuation character and add the rest of the string to the current index in our new array
        $unwrappedLines[$cursor] += $line.Substring(0,$line.Length - 2)
    }
    else
    {
        # Line is not to be continued, add value as-is to current index
        $unwrappedLines[$cursor] += $line
        # Then increment our index cursor and initalize the next string in the array
        $unwrappedLines[++$cursor] = ""
    }
}

如果文件足夠小,只需將其作為一個字符串讀取,並將所有_newlines替換為空。

(Get-Content -Raw "c:\temp\test.txt") -replace "_`r`n"

-Raw適用於3.0。 如果你沒有那么Out-String來救援。

(Get-Content "c:\temp\test.txt" | Out-String) -replace "_`r`n"

只需要找到一個后跟新行的下划線並刪除它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM