search string in a file and retrieve 10 lines including string line

Hi Guys,

I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same.

can anyone of you please let me know how to go about it ?

here's something to get you started

#!/usr/bin/perl

my $filename      = shift;
my $search_string = shift;
my $new_filename  = shift;
my @to_write;

open (FSEARCH, '<', $filename)  or  die "could not read file $filename : $! \n";
while (my $line = <FSEARCH>) {
    if ($line =~ m/$search_string/) {
        print "found $search_string \n";
    }
}
close (FSEARCH);

open (FWRITE, '>', $new_filename)  or  die "could not write file $new_filename : $! \n";
print FWRITE @to_write;
close (FWRITE);

__END__

q. what's not in here?

  1. when the search string is found, writing the next ten lines in the array
  2. write the new file only if search string is found in the given file
$ cat 1.txt
word1 line1 word2
word1 line2 word2
word1 line3 word2
word1 line4 word2
word1 line5 word2
word1 line6 word2
word1 line7 word2
word1 line5 word2
word1 line8 word2
word1 line9 word2

$ awk '/line5/ {for(i=0;i<4;i++) {print;if(!getline) exit;} }' 1.txt
word1 line5 word2
word1 line6 word2
word1 line7 word2
word1 line5 word2

Thanks a lot guys...

Now i have the code which search for the string and put it in diff file but its executing only once evn though the name is present many at times in the file... i guess i need to give a condition to search till file ends ...

ranjithpr;

I used awk command it works fine if i want to print but can u pls tell me how to use this command so that it will print it in a file ? and also i found out that if the search string contains double cotes perl code will exit saying syntax error ... any suggestion ?

am using it like this inside perl

system "( awk '/Error "no1"/ {for(i=0;i<12;i++) {print; if(!getline) exit;} }' filename)';

or shall i use it in this way open file and use awk command to push all needful data ?

open (FSEARCH, '<', $filename) or die "could not read file $filename : $! \n";
while (my $line = <FSEARCH>) {
if ($line =~ m/$search_string/) {
@to_write = $line;

open (FWRITE, '>', $new_filename) or die "could not write file $new_filename : $! \n";
print FWRITE @to_write;
print "\n\n string has been written to $new_filename\n\n";

    system "\(awk '/Error "no1"/ \{for\(i=0;i&lt;12;i\+\+\) \{print FWRITE;if\(!getline\) exit;\} \}' $filename\)";
    print "\\n Error Printed \\n";

close (FWRITE);
}
}
close (FSEARCH);

$ cat 1.txt
word1 line1 word2
word1 line2 word2
word1 line3 word2
word1 line4 word2
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 "line5" word2
word1 line8 word2
word1 line9 word2
$ awk '/\"line5\"/ {for(i=0;i<4;i++) {print;if(!getline) exit;} }' 1.txt > result.txt
$ cat result.txt
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 "line5" word2
$

Thanks a lot... it worked fine !!

---------- Post updated 2010-08-19 at 11:10 AM ---------- Previous update was 2010-08-18 at 03:33 PM ----------

Hi i have one more requirement ...

is there any way i can delete 10 no of lines ..... like 2 lines above line5 and 2 lines after line5 .... any suggestion will be really helpful ..

---------- Post updated at 02:24 PM ---------- Previous update was at 11:10 AM ----------

any suggestions pls ???

awk -v s=5 'NR<s-2 || NR>s+2' urfile

Easiest way would be:

Display N lines after match

The following example prints the matched line, along with the 3 lines after it.

A stands for after the matching line,3 fetch next 3rows

$ grep -A 3 -i "example" demo_text 

Display N lines before match

-B is the option which prints the specified N lines before the match.

$ grep -B 2 "single WORD" demo_text
# cat infile
word1 line1 word2
word1 line2 word2
word1 line3 word2
word1 line4 word2
word1 "line5" word2
word1 line6 word2
word1 line7 word2
word1 line8 word2
word1 line9 word2
# l=5;sed -e 'N;H' -e "`expr $l - 1`d" infile | sed  "`expr $l - 1`,$l d"
word1 line1 word2
word1 line2 word2
word1 "line5" word2
word1 line8 word2
word1 line9 word2

regards
@ygemici