我有一些命令列表,其中有些参数在执行前需要跳过。
最终,列表具有(),<>,[]与纯文本命令的不同组合。 我想从像“show abc”这样的普通命令中分离出所有其他命令。
命令需要处理: -
(h1), (h2), (a,l) are to be discarded
<32> - is to be replaced with any ip address
[<32>] - is to be replaced with any integer digit
我试过了,但结果文件是空的: -
cat show-cmd.txt | grep "<|(|[" > hard-cmd.txt
如何使用正则表达式获取没有普通命令的结果文件?
所需的输出文件: -
show abc xyz
show abc xyz opq
show abc xyz 1.1.1.1
show abc xyz 2 opq
答案 0 :(得分:1)
尝试使用grep
后跟sed
grep '[(<\[]' file | sed -e 's/\[<32>\]/2/g' -e 's/<32>/1.1.1.1/g' -e 's/([^)]*)//g'
输出:
show abc xyz
show abc xyz opq
show abc xyz 1.1.1.1
show abc xyz 2 opq
请注意,s///g
命令的顺序对您而言很重要。
同时尝试避免多余使用cat
答案 1 :(得分:0)
cat show-cmd.txt | grep "[\[\(\<]" > hard-cmd.txt
这应该有效。开始和结束方括号[]表示只需要存在一个选项。然后,您要搜索的其他括号将由。
提供和转义希望这会有所帮助。 Pulkit