Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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
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
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.

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 cooling their heels in the Monastery: (9)
As of 2014-12-26 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls