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

Hi Monks

I'm stuck trying to extract the Trailer count from a file that contains a Header and Trailer within brackets.

I have the following one-liner so far, but it doesn't quite get exactly what i need

perl -ne 'print "$1\n" if /\[(.*?)\]/;' <filename>

Sample input looks like this:

[HDR20140110230120] --data content-- [TRL345]

The data content is swift data and is not surrounded by brackets so is not relevant for this example.

My expected result would be 345 in this example. Also, other samples of what the trailer record could be are:

 [TRL0] or [TRL25678]

It's a variable length count with no leading zeroes.

Any help would be greatly appreciated

thanks

Replies are listed 'Best First'.
Re: extract trailer count one liner
by kennethk (Abbot) on Jan 13, 2014 at 17:38 UTC
    Since it looks like your hit contains the letters TRL followed by one or more digits, you could express this as
    perl -ne 'print "$1\n" if /\[TRL(\d+)\]/;' <filename>
    This will also strip off the unwanted alpha chars.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: extract trailer count one liner
by hdb (Monsignor) on Jan 13, 2014 at 17:38 UTC

    If you are only looking for the digits, try

    perl -ne 'print "$1\n" if /\[TRL(\d+)\]/;' <filename>
Re: extract trailer count one liner
by Anonymous Monk on Jan 13, 2014 at 18:15 UTC
    And if the file is large, seek() relative to the end of file before you start to read. Say the known max-length of a trailer plus 50 bytes. Then read lines until you find one that matches the trailer regex.