Home > Blockchain >  How to select a group lines by your content of a file in GNU/Linux?
How to select a group lines by your content of a file in GNU/Linux?

Time:05-21

Good days. I have a problem. I have that get the lines that has a content specified. The command grep allow search a content specified of a file, but this line by line. I would like to select a content of various lines. How to do this? something as

cat -n /etc/profile | grep "
   if [ "$DISPLAY" != "" ]
   then
       xhost  si:localuser:root
   fi
"

Thank you very much.

CodePudding user response:

Suggesting to research grep with option -z.


But better option is awk.

With awk it is possible to select a range awk '/RegExp1/,/RegExp2/' input.txt

In your case:

 awk '/if/,/fi/{print}' input.txt

Will print all lines in if fi range.


Also With awk it is possible to define record separator with RS variable. For example record separator is empty line. awk '1' RS="\n\n"

  • Related