Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Pattern Matching

by fatfinger (Initiate)
on Nov 19, 2008 at 13:15 UTC ( [id://724572]=perlquestion: print w/replies, xml ) Need Help??

fatfinger has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Pattern Matching
by Crian (Curate) on Nov 19, 2008 at 13:24 UTC

    I would suggest you to use strict; and use warnings;.

    There are missing the opening " characters in your error handling on opening the file.

    If I understand your problem, perhaps grep is the tool you are looking for, not Perl's grep, the one of your operating system.


    You can't use the same filehandle 'File' for two different files. And I suggest lexically filehandels.


    cnt++; is no Perl construct. I guess you want wto say $cnt++;. Id would be finde, if you would only show us code, that runs without errors and warnings.

Re: Pattern Matching
by svenXY (Deacon) on Nov 19, 2008 at 13:44 UTC
    ...My friend now wants to see more context...

    you mean friend as in teacher? ;-)

    Anyway, this question has been asked just a few days ago and you will probably be able to find it. But as you surely want to do your homework yourself, you should:

  • match against the pattern and print the previous line (from the variable where you stored it, see next bullet) and the current line
  • store the current line in a variable, so that you can print it in the next while-iteration if necessary
  • pull the next line from <File> and print it (but only if you matched)


    Regards,
    svenXY

    update: and don't forget to use strict and warnings as suggested by ++Crian. In addition, you might want to check if your input isn't something dangerous...

      You also have to take into account when two lines match in a row. What do you do then? Do you still print the previous line, even though it was already printed?
Re: Pattern Matching
by JavaFan (Canon) on Nov 19, 2008 at 14:08 UTC
    I'd just reuse existing code, and use grep -C 1 'PATTERN' file.

    grep has been ported to Windows as well.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://724572]
Approved by svenXY
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-19 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found