使用正则表达式获取命令列表

时间:2021-11-16 23:16:13

I have list of commands where some are having parameters which I need to skip before executing them.

我有一些命令列表,其中一些参数在执行之前需要跳过。

  • show abc(h2) xyz
  • show abc(h2)xyz
  • show abc(h2) xyz opq(h1)
  • show abc(h2)xyz opq(h1)
  • show abc(h2) xyz <32>
  • show abc(h2)xyz <32>
  • show abc(a,l) xyz [<32>] opq
  • show abc(a,l)xyz [<32>] opq
  • show abc
  • 显示abc

Ultimately, the list has different combinations of ( ), <>, [] with plain text commands. I want to separate out all other commands from plain commands like "show abc".

最终,列表具有(),<>,[]与纯文本命令的不同组合。我想从像“show abc”这样的普通命令中分离出所有其他命令。

Processing needed on commands :-

命令所需的处理: -

(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

I tried following but resultant file was empty :-

我试过跟随,但结果文件是空的: -

cat show-cmd.txt | grep "<|(|[" > hard-cmd.txt

How can I get the result file which has no plain commands using regex?

如何使用正则表达式获取没有普通命令的结果文件?

Desired output file :-

所需的输出文件: -

show abc xyz
show abc xyz opq
show abc xyz 1.1.1.1
show abc xyz 2 opq

2 个解决方案

#1


1  

Try using grep followed by sed

尝试使用grep,然后使用sed

grep '[(<\[]' file | sed -e 's/\[<32>\]/2/g' -e 's/<32>/1.1.1.1/g' -e 's/([^)]*)//g'

Output:

输出:

show abc xyz
show abc xyz opq
show abc xyz 1.1.1.1
show abc xyz 2 opq

Please note that order of s///g command matters in your case.

请注意,s /// g命令的顺序对您的情况很重要。

Also try avoiding redundant use of cat

还要尽量避免多余使用猫

#2


0  

cat show-cmd.txt | grep "[\[\(\<]" > hard-cmd.txt

This should work. The opening and closing square brackets [] mean that only one of the options need to be present. Then the further brackets that you want to search for are provided and escaped by a .

这应该工作。开始和结束方括号[]表示只需要存在一个选项。然后,您要搜索的其他括号由a提供并转义。

Hope this helps. Pulkit

希望这可以帮助。 Pulkit

#1


1  

Try using grep followed by sed

尝试使用grep,然后使用sed

grep '[(<\[]' file | sed -e 's/\[<32>\]/2/g' -e 's/<32>/1.1.1.1/g' -e 's/([^)]*)//g'

Output:

输出:

show abc xyz
show abc xyz opq
show abc xyz 1.1.1.1
show abc xyz 2 opq

Please note that order of s///g command matters in your case.

请注意,s /// g命令的顺序对您的情况很重要。

Also try avoiding redundant use of cat

还要尽量避免多余使用猫

#2


0  

cat show-cmd.txt | grep "[\[\(\<]" > hard-cmd.txt

This should work. The opening and closing square brackets [] mean that only one of the options need to be present. Then the further brackets that you want to search for are provided and escaped by a .

这应该工作。开始和结束方括号[]表示只需要存在一个选项。然后,您要搜索的其他括号由a提供并转义。

Hope this helps. Pulkit

希望这可以帮助。 Pulkit