0

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 opq(h1)
  • show abc(h2) xyz <32>
  • show abc(a,l) xyz [<32>] opq
  • show 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".

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 Answers 2

1

Try using grep followed by 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.

Also try avoiding redundant use of cat

Sign up to request clarification or add additional context in comments.

Comments

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 .

Hope this helps. Pulkit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.