Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Reading certain lines in a file

by Ras (Acolyte)
on May 24, 2001 at 18:10 UTC ( #82907=perlquestion: print w/replies, xml ) Need Help??

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

Forgive me if this is a stupid question but i want to be able to read certain lines in a file and print it to another file. For example:
FILE:
blah blah blah
blah blah blah
couldn't parse
line i want to print
blah blah blah
blah blah blah
The files is always in this formate where the line i want is right after the line couldn't parse. Is there anyway in perl to this quickly? Thanks you.

Replies are listed 'Best First'.
Re: Reading certain lines in a file
by davorg (Chancellor) on May 24, 2001 at 19:48 UTC
    while (<>) { unless (parse_line($_)) { my $line = <>; print $line; } }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Reading certain lines in a file
by AidanLee (Chaplain) on May 24, 2001 at 18:15 UTC

    I'm uncertain what your sentence here means:

    The files is always in this formate where the line i want is right after the line couldn't parse

    but as to a general method to use, consider this:

    use strict; open(FILE,"myfile") or die "couldn't open input file"; open(OUTFILE,">myotherfile") or die "couldn't open output file"; my @file_lines = <FILE>; for( @file_lines ) { print OUTFILE $_ if /matches_this_pattern/; }
Re: Reading certain lines in a file
by mr.nick (Chaplain) on May 24, 2001 at 18:21 UTC
    You can always use the Unix command grep if you have access to to it:
    grep -i -A 1 'couldn\'t parse' FILE.TXT | tail -1
Re: Reading certain lines in a file
by tachyon (Chancellor) on May 24, 2001 at 18:31 UTC

    This does what you want

    tachyon

    # I use the DATA filehandle here # you will need to do an # open(DATA,"<file.txt") || die "Oops $!\n"; # to open your file for reading use strict; my $line; while (<DATA>) { next unless /couldn't parse/; $line = <DATA>; chomp $line; last; } close DATA; if ($line) { print "found line: $line\n"; save($line); } else { print "no line found\n"; } sub save { open (FILE,">>c:/my_stuff.txt") || die "Oops $!\n"; print FILE "$line\n"; close FILE; print "Saved line\n"; } __DATA__ blah blah blah blah blah blah couldn't parse line i want to print blah blah blah blah blah blah
      I like your use of <DATA> in a quick example script. I think I'll adopt that concept.

      —John

Re: Reading certain lines in a file
by kilinrax (Deacon) on May 24, 2001 at 18:23 UTC
    Assuming your data file contains the exact content listed above, here's a quick one-liner to do the job:
    perl -ne 'if (/couldn\047t parse/) { $_ = <>; print }' test.txt
    Adjust as necessary ;)
Re: Reading certain lines in a file
by mrmick (Curate) on May 24, 2001 at 18:33 UTC
    How about the following(untested, of course):
    # read a file and set a flag (boolean) if pattern is found # we then will print the following line and reset the flag open(OUT,$outputfilename)||die "Could not open $outputfilename for out +put\n$!\n"; open(FILE,$inputfilename)||die "Could not open $inputfilename for read +ing\n$!\n"; while(<FILE>){ if($checked){ print OUT; undef $checked; next; } if(/pattern/){ $checked=1; next; } } close(FILE); close(OUT);

    Mick

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-12-01 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?