我正在尝试使用以下命令删除与我提供的详细信息匹配的行。当一个新文件output.txt
被创建时,它带有一个我不想来的新行。有没有办法在我下面粘贴的同一个命令中删除它。
a.txt
具有以下内容:
12345 asd 12-22-2009 23432 vfv 03-21-2019 23432 abcd 03-21-2019 38372 kcdsklfm 08-17-2011
Get-Content C:\test\a.txt |
Select-String -Pattern '23432' -NotMatch |
Out-File C:\test\output.txt
output.txt
(预期):
12345 asd 12-22-2009 38372 kcdsklfm 08-17-2011
output.txt
(当前):
12345 asd 12-22-2009 38372 kcdsklfm 08-17-2011
Select-String
不会产生字符串输出(例如参见here)。你得到的是空白行,因为`Select-String'中的MatchInfo
对象在写入输出文件时被破坏成一个字符串。
要避免此行为,请在将输出写入文件之前展开Line
对象的MatchInfo
属性:
Get-Content C:\test\a.txt |
Select-String -Pattern '23432' -NotMatch |
Select-Object -Expand Line |
Out-File C:\test\output.txt
您也可以选择不使用Select-String
并执行:
Get-Content C:\test\a.txt | Where-Object {$_ -notmatch '23432'} | Out-File C:\test\output.txt -Force