Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Search and Copy

by mikebailey (Initiate)
on Nov 23, 2012 at 15:11 UTC ( #1005278=perlquestion: print w/ replies, xml ) Need Help??
mikebailey has asked for the wisdom of the Perl Monks concerning the following question:

Need help searching a text file for a Word or String, then copy it and the next 250-300 characters into a new text file. Example: #website.txt This web site is privately owned and operated; all financial support comes from donations from individuals. There is little advertising on this web site except for a few small amateur-owned companies that manufacture boards and parts of interest to the community, and a few ads like the one above that help pay the hosting fees. Find the key word “web site” in the Paragraph then copy it and the next 250 characters to Web_Results.txt web site is privately owned and operated; all financial support comes from donations from individuals. There is little advertising on this web site except for a few small amateur-owned companies that manufacture boards and parts of interest to the community Would like to add some trailing characters at the end if possible, like (…) or (!!!) Thanks for the help….

Comment on Search and Copy
Re: Search and Copy
by kennethk (Monsignor) on Nov 23, 2012 at 15:35 UTC

    What have you tried? What didn't work? As well, please wrap sample input in <code> tags, so formatting is preserved. See How do I post a question effectively?.

    Your spec can be hit using regular expressions. Perhaps something like /(web site.{250})/i. You can then open your output file and print the contents of the capture buffer ($1).


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Assuming the specification is, “print the key phrase together with the following text up to 250 characters,” the regex would be better as /(web site.{0,250})/, which also matches when the key phrase is followed by fewer than 250 characters of text before the end of the file. As this match is greedy, it will match the largest number of characters up to 250.

      Athanasius <°(((><contra mundum

        This is what I have so far... open (OUTPUT, ">Results.txt") || die ("Could not open file results.txt; $OS_ERROR"); open( INFILE, "Textfile.txt" )or die("Can not open input file: $!"); while (<INFILE>) { if ($ARG =~ /Something/ ) { print OUTPUT $ARG ; } } close (OUTPUT);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-11-28 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls