Perl Script: Search for string from a folder with multiple files -


i have folder multiple .txt files in it. want check few strings in text files , give output out.txt 5 lines above , 5 lines below of located string.

if insist on perl oneliner;

perl -n -e 'if (/searchstringhere/) {print "\n\n\n\n\n$_\n\n\n\n\n"}' *.txt 

if grep solution works you, consider more elegant...

update

it struck me might windows user, don't have grep...

this code not tested, don't have perl installed on machine, should work: !#yourperl/perl

$flag, $l1, $l2, $l3, $l4, $l5, $l6; $cnt = 5; $file = shift; open("$file") || die "can't open $file\n"; while (<>) {  $l1 = $l2; # starting reserving lines print  $l2 = $l3;  $l4 = $l5;  $l5 = $l6;  $l6 = $_;  if (/your string here/) {    $cnt = 5;    print ($l1$l2$l3$l4$l5$l6);# print line + 5 prev lines    next  }  if ($cnt >0) { # print next 5 lines     print $l6;     $cnt--;  }  } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -