如何用Windows PowerShell删除逗号/分号与false间的字符串?
使用PowerShell删除逗号/分号与
false之间的字符串 需求:用Windows PowerShell删除所有位于英文逗号(,)或分号(;)与英文单词false之间的字符串。
输入内容
598842,3605false;3605true;36054true;36054true;360true;360false;36054true 598842, 3605true;36054true;36054true;360true;360false;36054true
期望输出
598842, 3605true;36054true;36054true;360true;36054true 598842, 3605true;36054true;36054true;360true;36054true
尝试过的方法(均未成功)
(Get-Content -Raw .\actswithpeople.csv) -Replace '.*?(\"totalCount\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -Replace '.*?(\"false;\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -Replace '\",\".*?(\"false;\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -Replace '.*?(\"false;\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s); \[false]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s);\[false]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s);\[false]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), ',(?s)\"[false]\"', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s): \[.+?\r?\n \]', { param($match) $match.Value -replace '[\r\n\"]' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)\[.+?\r?\n\]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;\[.+?\r?\n\]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;.+?\r?\n\', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;.+?\r?\n', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;.+?', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(?s)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(?)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo2.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.s)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(?,)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(?.)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<,.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<,.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII;(Get-Content -Raw .\noo.csv) -replace 'true','' | Out-File noo2.csv; (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<,.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII;(Get-Content -Raw .\noo.csv) -replace 'true','' | Out-File noo2.csv;(Get-Content -Raw .\actswithpeople.csv) -replace 'true','' | Out-File actsnotrue.csv (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<;.*?)[\r\n\"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<true;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.+?)(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.+?)(?<,.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<,.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(?<,.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<;.*?)(?=.*?false)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<;.*?)(?=false)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<;)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.?)(?<;)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<;)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '.(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII (Get-Content -Raw .\actswithpeople.csv) -replace '.*?(?<;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
解决方案
使用两次正则替换分别处理逗号和分号的场景:
(Get-Content -Raw .\actswithpeople.csv) ` -replace ',\s?[^,;]*false', ', ' ` -replace ';[^,;]*false', '' ` | Out-File -FilePath result.csv -Force -Encoding ASCII
正则说明
,\s?[^,;]*false:匹配逗号后紧跟的可选空格,再匹配任意非逗号、非分号的字符直到false,替换为,(保留逗号和空格,保证格式一致);[^,;]*false:匹配分号后任意非逗号、非分号的字符直到false,直接替换为空字符串,删除该段内容
内容的提问来源于stack exchange,提问作者Ne Mo
相关产品推荐
相关产品推荐

