1830939 Members
2652 Online
110017 Solutions
New Discussion

Re: Perl help

 
SOLVED
Go to solution
Vibhor Kumar Agarwal
Esteemed Contributor

Perl help

Hi,

I want to parse a file with perl.

a
b
c
d
e

Is the file and i want to have contents "b" to "d"

sed -n '/b/,/d/p' file.txt

does it.

How can it be done via perl.
Thanks
Vibhor Kumar Agarwal
10 REPLIES 10
Peter Godron
Honored Contributor

Re: Perl help

perl -n -e "print if /[b-d]/" file.txt
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl help

Hi:

Another way:

# perl -ne 'print if /b/../d/' file.txt

Regards!

...JRF...
spex
Honored Contributor

Re: Perl help

Hi Vibhor,

Just in case you were wondering about awk:
# awk '/b/,/d/' file.txt

PCS
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Perl help

Hi,

Peter, i think your command only works if the line starts with that. I had some words in mid-way.

James: Bingo
Vibhor Kumar Agarwal
Peter Godron
Honored Contributor

Re: Perl help

Sorry,
my mis-understanding of the input file.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Perl help

Welcome :-),

This is my first twist with perl.

One more hint needed.

How do i put it in perl script:

I was writing something like:

print "if '/\*\*\* BOM report/../----- BUILD/'" /usr/local/path/a.txt

Got error
Illegal division by zero.

What am i doing wrong.
Vibhor Kumar Agarwal
James R. Ferguson
Acclaimed Contributor

Re: Perl help

Hi (again):

# echo "\*\*\* BOM report" > myfile
# echo "da da da" >> myfile
# echo "---- BUILD/" >> myfile

# perl -nle 'print if /\\*\\*\\* BOM report/../----- BUILD/' myfile

Regards!

...JRF...
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Perl help

You got me wrong.

I have a huge file, midway it contains text like

blah
blah
*** BOM report
blah
---- BUILD
blah

This is what i am parsing.

I want to run that file from inside a perl script, as the file is getting generated from within the script itself.
Vibhor Kumar Agarwal
James R. Ferguson
Acclaimed Contributor

Re: Perl help

Hi:

You say that the file that you want to parse is "...generated within the script itself".

Is the script in question a Perl script or a shell script?

If it's a Perl script then close the file; reopen it; and parse it; something like this, perhaps:

...
open(F, "<", "/tmp/build") or die "Cannot open: $!\n";
while () {
$ok=1 if /BOM/;
print if $ok;
last if /BUILD/;
}
...

...If the script is a shell script, you can simply embed the perl snippet I offered within the shell script just like you would spawn an 'awk' or 'sed' snippet.

Regards!

...JRF...
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Perl help

Its a perl script.

Okay, i will try the Open-Close thingy and let you know.

Thanks
Vibhor Kumar Agarwal