首页 > 解决方案 > 使用 Powershell 将一行文本移动到其上一行的末尾

问题描述

我正在尝试找到使用 Powershell 将几行特定文本移动到其上方文本末尾的最佳方法。它正在抓取 CSV 的内容,并寻找有人在打字过程中按回车键的错误。

这是内容看起来有两个略有不同的问题。所有行应为五列长。您可以看到其中两条线已在中间分开。一个结尾有双引号,而另一个没有。

"WORDS","WORDS","WORDS","WORDS","WORDS"
"WORDS","WORDS","WORDS
","WORDS","WORDS"                       <--Line should be moved to the end of the line above.
"WORDS","WORDS","WORDS","WORDS","WORDS"
"WORDS","WORDS","WORDS"
","WORDS","WORDS"                       <--Line should be moved to the end of the line above AND it needs to throw out one of the double quotes.
"WORDS","WORDS","WORDS","WORDS","WORDS"
"WORDS","WORDS","WORDS","WORDS","WORDS"

我已经发布了我用来整理下面的 CSV 的代码。第一行通过修剪确保将任何单引号切换为双引号,并且行尾没有空格。我们得到许多格式奇怪的 CSV,其中混合了单引号和双引号,并且在某些行的末尾有大量空白。第二行应该找到以下模式 (NEWLINE)"," 和 "(NEWLINE)"," 并用 "," 替换每个模式,以便它正确地落后于它上面的行。

(Get-Content $File).trim() -replace("','",'","') -replace("^'|'$", '"') | Set-Content $File

(Get-Content $File -Raw) -replace("`"[`r`n]`",`"", '","') -replace("[`r`n]`",`"", '","') | Set-Content $File

第一行代码本身就可以很好地工作。

只要我不运行它之前的第一行代码,第二行代码的第二个 -replace 似乎就可以工作。这是一个问题,因为我需要确保在运行第二行代码之前对所有内容进行修剪并使用双引号。

我还没有能够让第二行代码的第一个 -replace 工作。我得到任何工作的唯一方法是转义每个双引号并将换行代码放在方括号中。有什么方法可以让所有这些正常工作吗?提前感谢您提供的任何帮助。

标签: regexpowershell

解决方案


在您的示例中,此解决方案有效。

$lines = @'
"WORDS","WORDS","WORDS","WORDS","WORDS"
"WORDS","WORDS","WORDS
","WORDS","WORDS"                       <--Line should be moved to the end of the line above.
"WORDS","WORDS","WORDS","WORDS","WORDS"
"WORDS","WORDS","WORDS"
","WORDS","WORDS"                       <--Line should be moved to the end of the line above AND it needs to throw out one of the double quotes.
"WORDS","WORDS","WORDS","WORDS","WORDS"
"WORDS","WORDS","WORDS","WORDS","WORDS"
'@ -split "`r`n"

for ($i = 0; $i -lt $lines.Count; $i++){
    if (($lines[$i] -split '","').Count -ne 5){
        if ($lines[$i].StartsWith('",')){
            $lines[$i-1].TrimEnd('"') + '"' + $lines[$i].TrimStart('"')
        }
    }
    else{
        $lines[$i]
    }
}

推荐阅读