Powershell 删除除包含特定字符串的行之外的所有行
Powershell Remove all lines except those containing certain strings
我可以使用书签和其他 Notepad++ 功能逐一执行此操作,但我会经常这样做以编辑文档。我已经使用下面的 powershell 删除了除包含特定字符串的行之外的所有行,但是我该怎么做,比如 50 个字符串。
$SourceFile = 'C:\PATH\TO\FILE.csv'
$Pattern = 'word||'
(Get-Content $SourceFile) | % {if ($_ -match $Pattern){$_}} | Set-Content $SourceFile
我猜 $Match
在你的例子中应该是 $Pattern
。
您可以在模式中指定多个关键字,如下所示:
$SourceFile = 'C:\PATH\TO\FILE.csv'
$Pattern = 'word|excel|powerpoint'
(Get-Content $SourceFile) | Where-Object { $_ -match $Pattern } | Set-Content $SourceFile
我可以使用书签和其他 Notepad++ 功能逐一执行此操作,但我会经常这样做以编辑文档。我已经使用下面的 powershell 删除了除包含特定字符串的行之外的所有行,但是我该怎么做,比如 50 个字符串。
$SourceFile = 'C:\PATH\TO\FILE.csv'
$Pattern = 'word||'
(Get-Content $SourceFile) | % {if ($_ -match $Pattern){$_}} | Set-Content $SourceFile
我猜 $Match
在你的例子中应该是 $Pattern
。
您可以在模式中指定多个关键字,如下所示:
$SourceFile = 'C:\PATH\TO\FILE.csv'
$Pattern = 'word|excel|powerpoint'
(Get-Content $SourceFile) | Where-Object { $_ -match $Pattern } | Set-Content $SourceFile