regex - 使用 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 工作。我得到任何工作的唯一方法是转义每个双引号并将换行代码放在方括号中。有什么方法可以让所有这些正常工作吗?提前感谢您提供的任何帮助。
解决方案
在您的示例中,此解决方案有效。
$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]
}
}
推荐阅读
- ios - 带有动态数据的 TableCell 内的 UICollectionView 不起作用
- xamarin.ios - 如何访问当前 UIView 之外的按钮?用户界面;Xamarin.ios 本机;
- c# - 如何使用 Bot Framework 在 Teams 中与自适应卡片一起添加提及
- awkward-array - 在笨拙数组备份的子集上传播选择
- c - Scheme 到 C 的翻译器
- mockito - 使用给定参数返回带有 Mockito 的模拟对象
- axapta - 在声明中更改allowedit
- typescript - 在 TypeScript 中使用数据库的 Firebase 管理员
- javascript - @babel/preset-env 没有从 package.config 中读取 browserslist
- php - SyntaxError:JSON.parse:在 json 输出中使用 URL 时需要