簡體   English   中英

Powershell 文本處理:加入 txt 文件的特定行

[英]Powershell text procesing: Join specific lines of a txt file

我必須處理一些文本並遇到一些困難:

text.\text.txt 的格式如下:

name,
surname,
address,

name.
surname,
address,

等等

我想要實現的是加入以“,”結尾的對象,如下所示:

name,surname,address

name,surname,address

ETC

我正在做這樣的事情:

$content= path to the text.txt
$result= path to the result file

Get-Content -Encoding UTF8 $content | ForEach-object {
        if ( $_ -match "," ) {
           ....join the selected lines.... 
        }
    } |Set-Content -Encoding UTF8 $result

我還需要考慮的是,以“,”結尾的行可能有一個空的下一行,它應該是$result中的 CR

您的所有條款都以 a 結尾,因此您可以使用正則表達式:

$content= "C:\test.txt"
$result= "path to the result file"

$CR = "`r`n"

$lines = Get-Content -Encoding UTF8 $content -raw 
$option = [System.Text.RegularExpressions.RegexOptions]::Singleline 

$lines = [regex]::new(',(?:\r?\n){2,}', $option).Replace($lines, $CR + $CR)
$lines = [regex]::new(',\r?\n', $option).Replace($lines, ",")

$lines | Out-File -FilePath $result -Encoding utf8

結果:

name,surname,address

name1,surname,address

name,surname,address

name,surname,address

您可以通過首先在空換行符上拆分數據塊來做到這一點:

# read the content of the file as one single multiline string
$content = Get-Content -Path 'Path\To\The\file.txt' -Raw -Encoding UTF8
# split on two or more newlines and dispose of empty blocks
$content -split '(\r?\n){2,}' | Where-Object { $_ -match '\S' } | ForEach-Object {
    # trim the text block, split on newline and remove the trailing commas (or dots)
    # output these joined with a comma
    ($_.Trim() -split '\r?\n' ).TrimEnd(",.") -join ','
} | Set-Content -Path 'Path\To\The\NEW_file.txt' -Encoding UTF8

Output:

name,surname,address
name,surname,address

下面的代碼將給出所需的結果。

$content= "Your file path"
$resultPath = "result file path"

 Get-Content $content | foreach {
 
  $data  = $_
  if($data -eq "address,")
  {
    $NewData = $data -replace ',',''
    $data = $NewData + "`r`n" 
  }
  $out = $out + $data
  
 }

$out | Out-File $resultPath

暫無
暫無

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

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