grep – show lines until certain pattern - Ask Ubuntu


this question has answer here:

i on ubuntu 16.04 , want search files let’s “if” , want output until “endif”. file this

if …   code endif 

i can grep -a9 if my_file | grep -b9 endif. doesn’t works if “if” clause larger 9 , if several “if” clauses in same file , if first grep command contains several “if” clauses. option -m1 in second grep doesn’t help. nested “if” clauses can ignored. has idea, maybe not grep?

difference how grep multiple patterns on multiple lines?

  1. the question asks solution grep answered in question: grep -a9 if my_file | grep -b9 endif. solution doesn’t work in case work in case of other question.

  2. the proposed grep solutions of other question don’t work (with ubuntu?): grep: ein nicht geschütztes ^ oder $ wird mit -pz nicht unterstützt. grep: not protected ^ or $ not supported -pz. use following:

    root:/tmp# cat file text begin text goes here.   end more text  root:/tmp# grep -pzo "^begin\$(.|\n)*^end$" file grep: ein nicht geschütztes ^ oder $ wird mit -pz nicht unterstützt 
  3. the proposed solutions search pattern start @ beginning of line if interpret proposed solution correctly. if remove ^ command doen't work.

you can use sed that:

sed -n '/if/,/endif/p' myfile 
  • -n don't print until ask it
  • /if/ find first line this
  • , keep going until...
  • /endif/ last line want
  • p print matched lines

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User