Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to read a text file, extract a records that contains a string, export the result into a temporary file

by tuakilan (Acolyte)
on Feb 14, 2008 at 09:11 UTC ( #667896=perlquestion: print w/ replies, xml ) Need Help??
tuakilan has asked for the wisdom of the Perl Monks concerning the following question:

hi all, I have a need to write a perl script to do the following
1. read a log file 2. search the logfile.txt for a specific string, in this case the stri +ng "TWO" 3. extract record that contains the string "TWO" from log file into an +other file
I tried this example but it does not work as what i wish
#!/usr/bin/perl -w use strict; use IO::File; use constant FILE => 'b2bclient-gateway-heartbeat.log'; use constant FIND => 'production'; IO::File->input_record_separator(FIND); my $fh = IO::File->new(FILE, O_RDONLY) or die 'Could not open file ', +FILE, ": $!"; $fh->getline; #fast forward to the first match, print each occurence in the file print IO::File->input_record_separator while $fh->getline; $fh->close;
====================================== The logfile.txt ooks like this ============================
2007-Nov-07 00:00:00 UTC (GMT +0000) - Poll: channel = ONE, ref = 2007-Nov-07 00:00:01 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:01 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:02 UTC (GMT +0000) - Poll: channel = THREE, ref = 2007-Nov-07 00:00:02 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:03 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:03 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:04 UTC (GMT +0000) - Poll: channel = TWO, ref =
Thanks - TuaKiLan tuakilan@gmail.com

Comment on How to read a text file, extract a records that contains a string, export the result into a temporary file
Select or Download Code
Replies are listed 'Best First'.
Re: How to read a text file, extract a records that contains a string, export the result into a temporary file
by hipowls (Curate) on Feb 14, 2008 at 09:34 UTC

    Or if you're on a windows machine without grep perl -ne'print if /TWO/' b2bclient-gateway-heartbeat.log > output.txt

    If it is part of a larger task then

    my $in_file = 'b2bclient-gateway-heartbeat.log'; open my $in_fh, '<', $in_file or die "Could not open file $in_file: $! +"; my $out_file = 'output.txt'; open my $out_fh, '>', $out_file or die "Could not open file $out_file: + $!"; while ( my $line = <$in_fh> ) { print {$out_fh} $line if $line =~ /TWO/; } close $in_fh or die "Could not close file $in_file: $!"; close $out_fh or die "Could not close file $out_file: $!";

      hipowls, thanks for your tip, it works for one of the tasks that i need to perform on the text file. regards - tuakilan
Re: How to read a text file, extract a records that contains a string, export the result into a temporary file
by svenXY (Deacon) on Feb 14, 2008 at 09:35 UTC
    Hi,
    your example makes it harder as necessary...
    Besides, it does not actually do something. I'd assume it doesn't even compile properly.
    Here's a working example (opening/closing the logfile is here replaced by reading from the DATA handle):
    use strict; use warnings; my $infile = 'b2bclient-gateway-heartbeat.log'; my $outfile = 'results.txt'; my($fh_out, $fh); open($fh_out, '>', $outfile) or die "Could not open outfile: $!"; # open($fh, '<', $infile) or die "Could not open logfile: $!"; # while (<$fh>) { while (<DATA>) { /TWO/ && print $fh_out $_; } close $fh_out; #close $fh; __DATA__ 2007-Nov-07 00:00:00 UTC (GMT +0000) - Poll: channel = ONE, ref = 2007-Nov-07 00:00:01 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:01 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:02 UTC (GMT +0000) - Poll: channel = THREE, ref = 2007-Nov-07 00:00:02 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:03 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:03 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:04 UTC (GMT +0000) - Poll: channel = TWO, ref =

    Regards,
    svenXY
Re: How to read a text file, extract a records that contains a string, export the result into a temporary file
by Punitha (Priest) on Feb 14, 2008 at 09:39 UTC

    Hi tuakilan, try this

    use strict; open(IN,'logfile.txt') or die("cannot open the log file for reading\n" +); open(OUT,'>output.txt') or die("cannot open the output file for writin +g\n");######To create the output file while(<IN>){######to read the input file print OUT "$_" if($_=~/\bTWO\b/i); } close(IN); close(OUT);

    Punitha

Re: How to read a text file, extract a records that contains a string, export the result into a temporary file
by apl (Monsignor) on Feb 14, 2008 at 10:51 UTC
    A minor tweak to a great solution by svenXY:
    while (<STDIN>) { /TWO/ && print STDOUT $_; }

    By the way,you really should go to your teacher and ask for a tutor.

Re: How to read a text file, extract a records that contains a string, export the result into a temporary file
by Anonymous Monk on Feb 14, 2008 at 09:21 UTC
    $ grep TWO logfile.txt >output.txt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2015-07-28 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (251 votes), past polls