perl - How to print all lines except the line before a pattern in file using awk -


how can awk?

input file

line 1 line 2 ####pattern####### (line 3) line 4 line 5 line 6 ####pattern####### (line 7) line 8 ####pattern####### (line 9) 

etc..

output file

line 1 line 3 line 4  line 5 line 7 line 9 

here 1 way awk:

awk '/pattern/{for(;i<nr-2;)print lines[++i];i=nr;delete lines;print $0}{lines[nr]=$0}' file 

output:

$ cat file line 1 line 2 ####pattern####### (line 3) line 4 line 5 line 6 ####pattern####### (line 7) line 8 ####pattern####### (line 9) $ awk '/pattern/{for(;i<nr-2;)print lines[++i];i=nr;delete lines;print $0}{lines[nr]=$0}' file line 1 ####pattern####### (line 3) line 4 line 5 ####pattern####### (line 7) ####pattern####### (line 9) 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -