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!