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
In reply to Using grep to pick out lines in a text file by Bunta
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |