Skip to main content
Tweeted twitter.com/StackUnix/status/732335812281552896
added 649 characters in body; edited tags
Source Link

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before each line that matches pattern ? For example, here is my input file

... ... 0.50007496 0.42473932 0.01527831 0.99997456 0.97033575 0.44364198 Direct configuration= 1 0.16929051 0.16544726 0.16608723 0.16984300 0.16855274 0.50171112 ... ... 0.50089841 0.42608090 0.01499159 0.99982054 0.97154975 0.44403547 Direct configuration= 2 0.16931296 0.16553376 0.16600890 0.16999941 0.16847055 0.50170694 ... 

I need a command that can give me back the 2nd line before the search string Direct configuration. I'm trying to run this in SUSE-Linux

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before each line that matches pattern ?

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before each line that matches pattern ? For example, here is my input file

... ... 0.50007496 0.42473932 0.01527831 0.99997456 0.97033575 0.44364198 Direct configuration= 1 0.16929051 0.16544726 0.16608723 0.16984300 0.16855274 0.50171112 ... ... 0.50089841 0.42608090 0.01499159 0.99982054 0.97154975 0.44403547 Direct configuration= 2 0.16931296 0.16553376 0.16600890 0.16999941 0.16847055 0.50170694 ... 

I need a command that can give me back the 2nd line before the search string Direct configuration. I'm trying to run this in SUSE-Linux

added 16 characters in body; edited tags; edited title
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

Print only the Nth line before the searcheach line that matches a pattern

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before the search patterneach line that matches pattern ?

Print only the Nth line before the search pattern

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before the search pattern

Print only the Nth line before each line that matches a pattern

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before each line that matches pattern ?

Source Link

Print only the Nth line before the search pattern

I'm trying to print only the <N>th line before a search pattern. grep -B<N> prints all the <N> lines before the search pattern. I saw the awk code here that can print only the <N>th line after the search pattern.

awk 'c&&!--c;/pattern/{c=N}' file 

How to modify this to print only the <N>th line before the search pattern