tags:

views:

38

answers:

6

How can I print all rows after matching pattern in nth row, but ignore all before matched row including the matching row, here is an example :

row1 something in this row
row2 something in this row
row3 something in this row
row4 don't need to match the whole line, something like java String.contains()
row5 something in this row
row6 something in this row
row7 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

I'm looking to print lines following the line which contains row4 anywhere in that row, is this possible with awk or sed, or any other way?

Output should be :

row5 something in this row
row6 something in this row
row7 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

I've seen similar question :

http://stackoverflow.com/questions/1761341/awk-print-next-record-following-matched-record

But I'm not sure how to adapt it to fit my needs.

A: 

You an use the following command:

grep -A 5 "row4" test.txt

The output will be:

row4
row5
row6
row7
row8
row9

-A goes for After and similary you can use -B key which goes for Before. 5 is the number of lines to output. You can choose whatever you want of course.

andr
A: 

This works, a guru could probably shorten it considerably:

#!/usr/bin/perl
$pat = shift(@ARGV);
$p = 0;
while(<>) {
  if ($p == 1) { print $_;}
  if ($_ =~ /$pat/) {
    $p = 1;
  }
}

From the cmdline:

$ ./p.pl dat4 datafile
Paul Rubel
+1  A: 

If Perl is fine with you can do:

perl -ne 'print if($f); $f=1 if(/row4/)'

Code in Action

codaddict
A: 

awk -v pattern="row4" 'go {print} $0 ~ pattern {go = 1}' input_file

glenn jackman
A: 
 awk 'c&&c>0;/row4/{c=5}' file
ghostdog74
A: 

Try this:

sed '1,/row4/d' inputfile
Dennis Williamson