Bunta has asked for the wisdom of the Perl Monks concerning the following question:
I am very, very new to Perl and programming, but have been asked by my company to learn the basics (by myself, with no texts!) to help in my job.
I am trying to pluck out all the lines containing a certain word from a large text file and copy them to a new text file.
I have succeeded in doing so, but I have realized that I need to also pluck out the line PREVIOUS to the each of the ones I have already been able to grab.
Here is the code I used to grab all the lines containing "adj : "
open(FILE, "<WordNet.txt"); my @array = <FILE>; close(FILE); open(FILE, ">>WordNetTest2.txt"); my @array2 = <FILE>; print "Extracting adjectives\n"; foreach $_ (@array) { @array2 = grep {$_ =~ "adj : "} (@array); print FILE "@array2"; close(FILE); exit; }
Here is a sample of what the lines look like from the text file:
-----------------
Fast
adj : quick, speedy
-----------------
I've grabbed all the "adj : " lines, but the others have eluded me!
Would there happen to be anyone who could instruct me on how to take out the previous lines along with all lines containing "adj : "?
Thank you very much in advance for any help.
-Brad
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using grep to pick out lines in a text file
by Cody Pendant (Prior) on Dec 19, 2008 at 03:21 UTC | |
by Bunta (Initiate) on Dec 19, 2008 at 05:05 UTC | |
Re: Using grep to pick out lines in a text file
by balakrishnan (Monk) on Dec 19, 2008 at 09:14 UTC | |
by lostjimmy (Chaplain) on Dec 19, 2008 at 16:28 UTC | |
Re: Using grep to pick out lines in a text file
by ig (Vicar) on Dec 19, 2008 at 16:52 UTC | |
Re: Using grep to pick out lines in a text file
by zentara (Archbishop) on Dec 19, 2008 at 17:14 UTC | |
Re: Using grep to pick out lines in a text file
by missingthepoint (Friar) on Dec 19, 2008 at 23:49 UTC |