Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Print some lines in a range

by ranrodrig (Novice)
on Mar 04, 2010 at 22:58 UTC ( [id://826837]=perlquestion: print w/replies, xml ) Need Help??

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

Folks, I'm running a code that writes a correct range of lines from a text file that is 1 GB but it never ends.

Can you help me in order to find out how can I stop it when it prints what I want? or give me a tip about how to do it using another method?

TIA for your help.

 perl -e "@lines=<>; print @lines[41900..50000];" -p data02.log > data03.log

Replies are listed 'Best First'.
Re: Print some lines in a range
by ikegami (Patriarch) on Mar 04, 2010 at 23:57 UTC

    -p + <>???
    -p + print???

    Your code should be

    perl -e"@lines=<>; print @lines[41900..50000];" data02.log > data03.lo +g

    As for your question,

    perl -ne"next if $.<41901; print; exit if $.==50001;" data02.log > dat +a03.log

    ($. is one-based while arrays are zero-based. That's why I changed the indexes by one.)

      Folks thanks a lot for your answers you saved my Soul & the best solution is the one that ikegami post

      time perl -ne"next if $.<41901; print; exit if $.==50001;" data02.log > data03.log

      real 0m0.26s

      The next one also works but is slower to run

      time perl -ne "print if 41901 .. 50001" data02.log > data03.log

      real 0m33.21s

      An this one runs too but it takes a lot of time to run

      time perl -e"@lines=<>; print @lines[41900..50000];" data02.log > data03.log

      real 1m9.42s

        The first stops when enough has been read. Both the second and the third read the entire file. The difference is that the third loads the entire file into memory. And since we're talking about at least 50,000 lines, you might be running into swapping issues. The third definitely has a lot more memory allocations (one per line) than the second (almost none), and those aren't cheap.

Re: Print some lines in a range
by chuckbutler (Monsignor) on Mar 05, 2010 at 01:02 UTC

    There is a conflict between using command line option, switch, '-p' which sets up a loop around the '-e' code and using '@lines=<>' which reads the whole file(s), know as a slurp, into the list. A '-p' loop looks like:

    LINE: while (<>) { #readline into $_; diamond operator ...<your '-e' code here>... } continue { print or die "-p destination $!\n"; }

    Depending on what you are doing, the following may be better:

    perl -pe "last if ($cnt > 50000);$_='' if ($cnt++ < 41900);" data02.lo +g > data03.log

    This one-liner prints record 41900..50000, zero relative. It will end on record 50001, which should let you have the target file a little sooner.

    Good luck. -c

Re: Print some lines in a range
by jwkrahn (Abbot) on Mar 04, 2010 at 23:53 UTC
    perl -ne "print if 41901 .. 50001" data02.log > data03.log
      Nice, but that doesn't stop reading the input file after line 50001 as the OP asked.
Re: Print some lines in a range
by snape (Pilgrim) on Mar 05, 2010 at 04:21 UTC

    1. To stop it from running : Cltr C

    2. Another Method:

    Steps:

    a. Since the file is large, read each line of the file using while loop. Don't insert the entire file in an array.

    b. Then, use count to keep track of each line of the file and store the line using push. In this case, you need to know which lines you to print/have in an array (or any data structure depending upon your later operations)

    or use regular expressions to extract the line you want or the one you want to discard.

    Example Code: Insert the Lines using regular expression

    print "Enter the file name: "; chomp( $filename = <STDIN> ); open my $INPUT, $filename or die $!; my $count = 0; while(<$INPUT>){ $count++; if ( $_ =~ <pattern> ){ push(@lineCases,$_); } }
Re: Print some lines in a range
by sweetblood (Prior) on Mar 04, 2010 at 23:39 UTC
    ctrl-c

    Sweetblood

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 04:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found