Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Reading a file from any arbitrary place in the file.

by Anonymous Monk
on Nov 21, 2005 at 19:08 UTC ( #510524=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am trying to read a file in which I have to ignore all the read lines till I reach a particular line and from that line onwards, I want to write it to a file. For example
ahdhdklsklfhklfhfl hklhifdhfeihfiohfhf hlhfdlhdfdhflfhhfhf oioooooooooooooo This is the temp line. aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa sssssssssssssssssssss
Now in the above example file I want to check for "This is the temp line." and then I want to write the lines following the above text. Is there a way other than doing a reverse read. Please guide.Thanks

Comment on Reading a file from any arbitrary place in the file.
Download Code
Re: Reading a file from any arbitrary place in the file.
by Anonymous Monk on Nov 21, 2005 at 19:23 UTC
    Do you mean to print all of the lines after "This is the temp line."? If so, this should do the trick:
    $print = 0; while ( <DATA> ) { if ( $print ) { print; } if ( /^This is a temp line\./ ) { $print = 1; } }
      Sorry, typed too fast; the regex should be "This is the temp line". I'm sure you get the idea, though.
Re: Reading a file from any arbitrary place in the file.
by davidrw (Prior) on Nov 21, 2005 at 19:36 UTC
    Basic approach is to read through the while file, and set the flag once you hit your marker, and print if the flag is set.
    perl -ne 'if($ok){print;}else{$ok=/^This is the temp line\.$/}' foo.tx +t
    Another approach, if the file is small enough, is to slurp into memory and split it, using your marker as the delimiter:
    perl -n0777e 'print ((split(/^This is the temp line.\n/m, $_, 2))[-1]) +' foo.txt
Re: Reading a file from any arbitrary place in the file.
by diotalevi (Canon) on Nov 21, 2005 at 19:38 UTC

    The following code uses readline's use of $/ as the line ender.

    { # Temporarily set $/. local $/ = "This is the temp line.\n"; <>; } while ( <> ) { print; }

    Thanks Roy Johnson for the tip on the spelling error.

Re: Reading a file from any arbitrary place in the file.
by chibiryuu (Beadle) on Nov 21, 2005 at 19:51 UTC
    1 .. /^This is the temp line\.$/ or print while <>;

    Use of the range operator in scalar context, Yay!

    If you want to include "This is the temp line", you can do this:

    /^This is the temp line\.$/ .. 0 and print while <>;

Re: Reading a file from any arbitrary place in the file.
by TedPride (Priest) on Nov 21, 2005 at 23:32 UTC
    use strict; use warnings; my ($line, $length, $in, $out, $inh, $outh); $line = 'This is the temp line.'; $in = 'in.dat'; $out = 'out.dat'; $length = length($line) + 1; open ($inh, $in); open ($outh, ">$out"); while (<$inh>) { last if $length == length($_) && index($_, $line) != +-1; } while (<$inh>) { print $outh $_; } close($outh); close($inh);
Re: Reading a file from any arbitrary place in the file.
by Anonymous Monk on Nov 22, 2005 at 00:41 UTC
    If your description is the entire problem, I would suggest using sed instead of Perl because it has syntax for dealing with exactly this kind of operation.
    sed -n "/This is the temp line/,$"p infile > outfile
    I'm not hating on Perl by any means, I just use the simplest tool for the job. Of course if you have to do anything more than a regex or two with the text before generating the output, stick with Perl.
      Or, if you do not want to include the temp line:
      sed -n "1,/This is the temp line/"!p infile > outfile
Re: Reading a file from any arbitrary place in the file.
by Knom (Beadle) on Nov 22, 2005 at 01:45 UTC
    use strict; use warnings; my ($trigger, $fh_in, $fh_out); my $split_text = 'This is the temp line.'; my $in = 'input_file.dat'; my $out = 'output_file.dat'; open ($fh_in, $in) or die "ERROR: opening file $in: $@\n"; open ($fh_out, ">$out") or die "ERROR: opening file $out: $@\n"; undef $trigger; while (<$fh_in>) { unless($trigger || /$split_text/) { next; } # END unless($trigger || /$split_text/) if(/$split_text/) { $trigger=1; next; # Else delete or comment out this line if you want the + split_line printed. } # END if(/$split_text/) print $fh_out $_; } # END while (<$fh_in>) close($fh_in); close($fh_out);
Re: Reading a file from any arbitrary place in the file.
by bfilipow (Monk) on Nov 22, 2005 at 09:26 UTC

    What about a one-liner like:

    ~$ cat monks001.txt hdhdklsklfhklfhfl hklhifdhfeihfiohfhf hlhfdlhdfdhflfhhfhf oioooooooooooooo This is the temp line. aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa sssssssssssssssssssss ~$ perl -ne 'print unless 1 .. /temp/' monks001.txt aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa sssssssssssssssssssss ~$
Re: Reading a file from any arbitrary place in the file.
by Anonymous Monk on Nov 22, 2005 at 09:28 UTC
    #!/usr/bin/perl open(DATA,"mahi"); $a=""; $a=$a.$_ while($_= <DATA>); $a =~ /This is the temp line\./g; print $`;

    Janitored by holli. Added code tags

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-10-31 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls