Hey Monks...I have a Perl question that deals with a huge
text file. I currently have a script that opens a text file, prompts a user for input, and reads in the lines
looking for the users input (pattern). When the script finds the users input or pattern, it extracts that line
and prints it to an outfile. Pretty straight forward.
#Current Script
<br>open(File, "<U:\\PerlFolder\\test.txt") or die (Can't open $FILE")
+;
open(File, ">U:\\PerlFolder\\results_Out.txt") or die (Can't open $FIL
+E");
print"Enter Term\n";
$input=<STDIN>;
chomp($input);
while($lines=<File>)
{
chomp ($lines);
cnt++;
if($lines=~/$input/i)
{
print OUTFILE"$lines\n";
}
}
close (FILE);
Question:
My friend now wants to see more context not just the line where the term resides. Basically I need to find the text above and the text below the line that contains the user input? One other thing to mention ...this input file is blocks of text/sentences/phrases/embedded whitespaces, no real defined format.
Might there be a way to manipulate the above script to do what I need to do or do I need to go a different direction all together?
Any help or code samples you guys could provide would be great.
Thanks Andrew
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.