in reply to extract (a range of) numbered lines from a file

sorry but the fun to produce oneliners is something i cannot resist..

I ended with a wonderful (;=)) one if you accept to pass range à la Perl 1,2,4..5 instead of hyphenated ones like 1,2,4-5

I'm very proud of the solution to workaround the off by one error between line number (from 1) and array elements (from zero): a two char fix 0, was enough.

For the joy of many, another evil use of eval (warning: win32 doublequotes!)

perl -e "BEGIN{$w=pop @ARGV}print +(0,<ARGV>)[eval $w]" linenumb +er.txt 1,3..5

L*

PS even simpler:

perl -e "print +(0,<ARGV>)[eval pop]" linenumber.txt 1,3..5

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.

Replies are listed 'Best First'.
Re^2: extract (a range of) numbered lines from a file -- smarter oneliner 22chars
by shmem (Chancellor) on Sep 21, 2016 at 16:31 UTC

    Nice!

    perl -e "print +(0,<ARGV>)[eval pop]" linenumber.txt 1,3..5

    You can omit the ARGV, and the blank after print, too:

    perl -e "print+(0,<>)[eval pop]" linenumber.txt 1,3..5

    22 :-)

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      Let's say 20..
      perl -E "say+(0,<>)[eval pop]" linenumber.txt 1,3..5

      I was sure to have tested the empty diamond, but evidently not.. ;=)

      L*

      PS 19 chars if I can cheat:

      perl -snlE "say if $.~~[eval$R]" -- -R="2,3..5" linenumber.txt

      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.