0

I have an issue in which my string its not a single string; it is a string with multiple words; in which using sed works great; but not with awk;

How can I group all that string with awk? I tried to encapsulate the string with " ", with { }, with ( ) and nothing works. Here is the example of my sed vs awk:

sed -n '/show system | inc Software/,/sh syst | i @@@@@/p' filename.txt > out_file.txt

That line works perfect with sed; the strings are "show system | inc Software" and "sh syst | inc @@@@@"

As for awk it doesnt work.

awk '/show system | inc Software/,/sh syst | i @@@@@/' filename.txt > out_file.txt

The output file is not what I expect. How can I do that strings to work in that command?

2
  • 1
    I think, by default awk will use extended regular expressions, so the patterns will match lines with either side of the |. You need to escape it as \| to match a regular bar.
    – Philippos
    May 15, 2019 at 16:35
  • Great. Thanks for that... Adding the escape "\" it makes it works. - awk '/show system \| inc Software/,/sh syst \| i @@@@@/' -> (works). - awk '/show system | inc Software/,/sh syst | i @@@@@/' -> (doesnt work) May 16, 2019 at 17:37

1 Answer 1

0

Your question will be easier to understand if you include your input data and expected output data.

The following awk command will print the lines 'show system | inc Software' and 'sh syst | i @@@@@' and every line in-between:

awk '/show system \| inc Software/,/sh syst \| i @@@@@/{n=0}' filename.txt > out_file.txt

Edit:

Also, it looks like your sed command won't work, (assuming I've understood your question) it should be:

sed -n '/show system | inc Software/,/sh syst | i @@@@@/p' filename.txt > out_file.txt

3
  • Hi Rusty. You are right; I forgot the -n flag at start and at the end it was /p flag and not /f. My bad..... it supposed to be -> sed -n '/show system | inc Software/,/sh syst | i @@@@@/p' filename.txt > out_file.txt May 16, 2019 at 17:27
  • Your long awk command works exactly as this one I had but updated with the escape -> awk '/show system \| inc Software/,/sh syst \| i @@@@@/' May 16, 2019 at 17:43
  • @JavierGonzalez Oh yes, so it does. Answer updated. May 17, 2019 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .