go ahead... be a heretic PerlMonks

### Search and Copy

by mikebailey (Initiate)
 on Nov 23, 2012 at 15:11 UTC 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….

Replies are listed 'Best First'.
Re: Search and Copy
by kennethk (Abbot) 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);

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005278]
Approved by muba
help
Chatterbox?
 [Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file. [LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy [Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history LanX damns the cult of CB ;-) LanX WTF WTF WTF [LanX]: please forget my last 3 posts [LanX]: Yeah option a doesn't go into history [LanX]: probably I need to teach the app to restart after C-c Kill [Corion]: LanX: Maybe have an infinite-loop cmd file? Much easier than trying to manage that from within Perl IMO [Corion]: Alternatively, relaunch the application from cron (or a Windows cron) every minute

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2017-03-27 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.