在Windows中删除文本文件中的特殊字符

问题描述 投票:1回答:1

select-string -pattern '^[#;] grep:

grep -ve ^# -ve '^;' -ve ^$ /name of file.

我没有成功找到解决办法。我正在尝试使用Powershell,但没有什么使用经验。

powershell
1个回答
1
投票

使用 Select-String 代替 grep:

Select-String -Path 'path/to/file' -Pattern '^[#;]|^$' -NotMatch 

Select-String 将输出一个 Match 对象,如果你只想得到匹配的字符串,就抓一个 Line 属性。

Select-String ... |Select -Expand Line

从PowerShell 7.0开始,你也可以使用 -Raw 转为 Select-String 只返回匹配的字符串,不返回其他内容。

Select-String ... -Raw

0
投票

这个很接近. 你可以用逗号隔开一个模式数组。 就像在bash中一样,分号必须用引号,因为它在powerhell中表示 "语句结束"。 该命令避免了以 "#"、"; "或空白开头的行。

'# comment',
'; semicolon',
'',
'one',
'two',
'three' | select-string ^#, '^;', ^$ -notmatch

one
two
three


0
投票

这是我用来输出我想要的结果; get-childitem zabbix_agentd.conf

© www.soinside.com 2019 - 2024. All rights reserved.