我想验证某一行上方的行是否存在,如果存在则确认。如果不存在,则删除该行并删除空白处?这可以在 powershell 中完成吗?请帮忙
如果在所有以“^37”开头的行之前,如果行以 33 开头,则 OK。否则,删除该行并删除空白处。现在,我的文件中有十多万个类似问题。请帮忙...非常紧急。
TestData.txt文件:-
03,201779,,01354,73923309,,,TEST2,7962753,,,0343,5087632,,/#end of line
04,399,777873,,,,text234,,,,/
33,TEST1,,,0343,,93493,,,343,,,,TEST3,,,,,,/
37,TEST37,text
49,24605597,6,343,343,343,,,3434,,,/
答案1
尝试以下 cmdlet:
#Get the file's content and join each line with newline character
#because by default splits lines by newline. So, the need to rejoin the lines
$text = [string]::Join("`n", (Get-Content a.txt))
#Find and replace the pattern, then output the result to file
#The file's content is replaced
[regex]::Replace($text, "^3[^3].+`n^(37.+)", '$1', "Multiline") | Out-file a.txt
替换a.txt
为您的文件名。