Languages and Scripting

Perl script to print lines in file...

 
SOLVED
Go to solution
Hazem Mahmoud_3
Respected Contributor

Perl script to print lines in file...

We are trying to print 5 lines before and 5 lines after a pattern that we search for in a file. We need to use perl (shell-script won't work b/c file is not a regular ascii file). A bunny for who can help me. Thanks!

-Hazem
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Perl script to print lines in file...

You need to do a little bit better job of defining the problem because when you say it is not a regular ASCII file that implies that line numbers have no meaning. Are these fixed-size records? Is there a record delimiter? You might mean that in these otherwise ordinary text line there are also non-printable characters embedded.
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Perl script to print lines in file...

For an ascii file the script would be-

while() {
$found=$. if /expression/;
if ($found) {
print @a if @a;
undef @a;
print $_ if $found+5 > $.;}
} else {
push(@a,$_);
shift(@a) if @a > 5;
}
}

HTH

-- Rod Hills
There be dragons...
Hazem Mahmoud_3
Respected Contributor

Re: Perl script to print lines in file...

Rodney, I think you are on to something. What we need is to modify the output to print a bunch of "===============" for example so we can separate the top 5 and bottom 5 lines into one segment. Can you help us with that?
For example:

==============
line1
line2
line3
line4
line5

line6
line7
line8
line9
line10
===============

Clay, it is an ascii file, but it has a few weird binary characters in it and there are also no new lines in it either. Thanks you guys!

-Hazem
A. Clay Stephenson
Acclaimed Contributor

Re: Perl script to print lines in file...

But don't you see that when you say there are no new lines (ASCII LF's and/or CR's) that, by definition, there are no lines in your file so that 5 lines before or 5 lines after some pattern has no meaning?

You could change the definition of the problem to something like N characters (more accurately N bytes) before some pattern and N characters after some pattern.

At this point, you are simply not describing the problem well enough. Of course, in many cases when the problem is ddescribed well, the solution is then very simple --- and often obvious.
If it ain't broke, I can fix that.
Sergejs Svitnevs
Honored Contributor

Re: Perl script to print lines in file...

#!/usr/bin/perl -w
open INP,'your_file';
print "\n==============\n";
while() {
$found=$. if /pattern/;
if ($found) {
print @a if @a;
undef @a;
print $_ if $found+6 > $.;
}
else {
push(@a,$_);
shift(@a) if @a > 5;
}
}
print "==============\n\n";
close INP;


Regards,
Sergejs
Hazem Mahmoud_3
Respected Contributor

Re: Perl script to print lines in file...

Sergejs, that just puts ========= at the beginning and end of the whole output. We want it at the beg and end of each 11 line segment.
Clay, ok, let's assume it is an ascii file. The problem is that when we tried using awk, it was not able to interpret the file. So, all we know is that perl can match the pattern we want, and awk cannot. I apologize for not explaining it correctly. So assuming it is an ascii file, how can we do that in perl. Thanks!

-Hazem
Rodney Hills
Honored Contributor
Solution

Re: Perl script to print lines in file...

Maybe this version will do what you want...

$rng=5;
while() {
$found=$. if /expresson/;
if (! $found) {
push(@a,$_);
shift(@a) if $a > $rng;
} elsif ($found+$rng > $.) {
print "=========\n";
$found=0;
} else {
if (scalar @a) {
print "=========\n";
print @a;
undef @a;
}
print $_;
}
}

Rod Hills
There be dragons...