Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to read multiple line from a file

by skyworld_chen (Acolyte)
on Apr 13, 2013 at 02:27 UTC ( [id://1028477]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

is there a way to read multiple lines from an opened file to a context variable? or is there a way to search multiple lines from a file each time? thanks.

Replies are listed 'Best First'.
Re: how to read multiple line from a file
by TJPride (Pilgrim) on Apr 13, 2013 at 06:16 UTC
    If you really need a line-based approach rather than just loading the whole thing into memory (most of the time, the latter approach will work just fine), then you need to cache the previous lines and do a search of the cache:
    use strict; use warnings; my $csize = 2; ### Number of lines to save my (@cache, $str, $c); my $file = 'data.txt'; open (IN, $file) or die "Can't open $file for read."; while (<IN>) { shift @cache if $#cache == $csize - 1; push @cache, $_; $c++; $str = join '', @cache; if ($str =~ /jumped\nover/) { print "String found in $file at lines " . ($c - $#cache) . "-$ +c:\n"; print $str; last; } }
    Input file data.txt:
    the quick brown fox jumped over the unfortunate dog
    Output:
    String found in data.txt at lines 5-6: jumped over
      Just a minor note from the C programmer in me: the easiest and most efficient way to do a fixed-size queue is to use the mod operator:
      while (<>) { $buf[$. % $size] = $_; }
        As a minor note of a functional programmer, I'll put the logic in an extra iterator like here: Re: Split string after 14 Line Feeds?.

        This makes the code much more readable. =)

        Otherwise I'm not sure where you want the processing logic to happen in your code, maybe something like unless ($. % $size) { ...} within the loop?

        I think If a sliding window is needed, shift and push might be better.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        How does this adapt to variable length records/lines/data?

        If you didn't program your executable by toggling in binary, it wasn't really programming!

      thanks for all of your replies. I will check these ideas again.
Re: how to read multiple line from a file
by 2teez (Vicar) on Apr 13, 2013 at 05:46 UTC

    Does this work for you?

    use warnings; use strict; use feature 'say'; my $str = do { local $/; <DATA>; }; say join " " => grep { $_ if /fox/ .. /over/ } split /\n/, $str; __DATA__ the quick brown fox jumped over the unfortunate dog
    Output
    fox jumped over

    NOTE:
    I used Athanasius data since the OP has none.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: how to read multiple lines from a file
by Athanasius (Archbishop) on Apr 13, 2013 at 03:09 UTC

    One approach is to use the core module Tie::File. For example, if you have an input file “data.txt” containing:

    the quick brown fox jumped over the unfortunate dog

    Then this script:

    #! perl use strict; use warnings; use Tie::File; my $filename = 'data.txt'; tie my @lines, 'Tie::File', $filename or die "Cannot tie file '$filena +me': $!"; chomp(my @text = @lines[3 .. 5]); print "\n", join(' ', @text), "\n"; untie @lines;

    will produce this output:

    13:01 >perl 605_SoPW.pl fox jumped over 13:06 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      thanks Athanasus. The way you provide is good, but if I want to search a special word across several lines within a file, for example, if a line contains a word "one" and the line next to it contains a word "two", this "tie" seems not as good as I expect. anyway, thanks for your idea

        Hello skyworld_chen,

        The solutions by 2teez and TJPride below are good ones, but if you want to adapt my Tie::File approach, just use a sliding window:

        #! perl use strict; use warnings; use Tie::File; my $filename = 'data.txt'; tie my @lines, 'Tie::File', $filename or die "Cannot tie file '$filena +me': $!"; my @search_words = ('jumped', 'over'); for (0 .. $#lines - 1) { if ($lines[$_ ] =~ /$search_words[0]/ && $lines[$_ + 1] =~ /$search_words[1]/) { print "\nFound '$search_words[0]' on line ", ($_ + 1), ", and '$search_words[1]' on line ", ($_ + 2), "\n"; last; } } untie @lines;

        Output:

        16:02 >perl 605_SoPW.pl Found 'jumped' on line 5, and 'over' on line 6 16:03 >perl 605_SoPW.pl

        Your precise requirements are not clear (to me), but this technique can be easily adapted as needed.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: how to read multiple line from a file
by roho (Bishop) on Apr 13, 2013 at 12:06 UTC
    Take a look at 'natatime' in List::MoreUtils.

    "Its not how hard you work, its how much you get done."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1028477]
Approved by Athanasius
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-18 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found