Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Keep only X number of entries of a file using a delimeter

by Anonymous Monk
on Oct 13, 2015 at 13:30 UTC ( [id://1144698]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!
I have a big file that contains several thousands of entries, which are separated by the delimiter "#". What must I write to keep the first, say, 500 entries? I know how to split this file into 1 file per entry with awk for example, but I can't find a way to just keep a number of entries and discard the rest.
  • Comment on Keep only X number of entries of a file using a delimeter

Replies are listed 'Best First'.
Re: Keep only X number of entries of a file using a delimeter
by choroba (Cardinal) on Oct 13, 2015 at 13:39 UTC
    I imagine your input looks like this:
    # Entry 1 ... # Entry 2 ...

    You can use $/ aka the Input record separator:

    perl -pe 'BEGIN { $/ = "#" } exit if $. > 500' input-file

    $. contains the record number.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Hi,
      interesting approach! Could you please re-write this to a "normal" script (i.e. not one-liner)?
        Sure. Just add
        -MO=Deparse
        before the switches.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Keep only X number of entries of a file using a delimeter
by hdb (Monsignor) on Oct 13, 2015 at 13:39 UTC

    Use a counter! Set $/ to "#" to read the desired chunks.

    use strict; use warnings; my $keep = 3; local $/="#"; while(<DATA>){ last unless $keep--; print; } __DATA__ 1 # 2 # 3 # 4 # 5

      You could also test $., like so: last if $. > 3;

      Thanks a bunch!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found