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



我想在Windows脚本中从文本文件输出文本,就像在Linux中使用grep:一样

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

我还没有成功地找到解决方案。我正在尝试使用Powershell,但使用经验很少。

使用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

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

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

这是我过去输出的东西;获取子项zabbix_agented.conf |选择字符串-模式'^[#;]|^$'-不匹配

这就是我所追求的,没有所有被注释掉的行和空格。zabbix_agented.conf:24:LogFile=C:\Program Files\zabbix Agent\zabbix-agented.logzabbix_agented.conf:88:服务器=10.0.0.22zabbix_agented.conf:130:ServerActive=10.0.0.22zabbix_agented.conf:141:主机名=RED-DOUGzabbix_agented.conf:257:Include=C:\Program Files\zabbix Agent\zabbix-agentd.conf.d\

谢谢你的帮助,Doug

最新更新