in reply to how to find the length of any line of file

Hi lakshmikant,

going into a completely different direction, a one-liner doing the job:

$ echo 'heaven > heavenly > heavenns > abc > heavenns > heavennly' | perl -nE 'BEGIN {$nr = shift;} chomp; say length $_ i +f $. == $nr;' 4 3

Replies are listed 'Best First'.
Re^2: how to find the length of any line of file -- oneliner
by Discipulus (Canon) on Nov 23, 2017 at 08:52 UTC
    He! no need of BEGIN block nor say and chomp

    perl -lne  "print length and exit if $. == 4"

    the above will work with data passed via pipe and also with a filename passed as argument.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Yes, Discipulus, no need for a begin block because you're hard coding the line number, whereas I wanted to pass it as an argument to the script and therefore needed to retrieve it (only once). Now, of course, for a one-liner, you might as well hard code the line number, it doesn't make a big difference.
        Hello Laurent_R, curiously enough (for me!)

         perl -lne  "print length  and exit if $. == $ARGV[0]"  datatest.txt 4

        does not complains about: Can't open 4: No such file or directory at.. and works even without the BEGIN block

        It just runs ok with both passed as arguments, not with the data passed via pipe though.

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.