Ever wanted to get a range of lines extracted from some file? Easy: load into editor, highlight lines, copy (usually Ctrl<c>), go to target, paste (usually Ctrl<v>).

You want to do that from the command line? With UNIX/Linux you have some options, combining the output of wc -l with head and tail.
You could also combine sed and awk (TIMTOWTDI applies):

sed -e '10,15p;4p;s/.*//' file | awk '!/^$/{print $0}' somefile

I'm not aware of Windows tools to do this task.
But anyways, this is unwieldy, specially if you want to read piped input into your editor of choice calling an external command.
Perl to the rescue:

#!/usr/bin/perl -n my $usage; BEGIN { $usage = "usage: $0 linespec file\n" . "linespec example: 2,5,32-42,4\n" . "this extracts lines 2,4,5 and 32 to 42 from file\n"; $spec=shift; die $usage unless $spec; @l=split/,/,$spec; for(@l){ ($s,$e)=split/-/; $e||=$s; $_=[$s,$e]; } } CHECK { unless(@ARGV) { push @ARGV, <DATA>; chomp @ARGV; } die $usage unless @ARGV; $file = $ARGV[0]; } # === loop ==== for $l(@l){ print if $.>=$l->[0] and $.<=$l->[1] } # === end === # END { if ($file) { open $fh,'<', $0; @lines = <$fh>; close $fh; open $fh,'>',$0; for(@lines){ print $fh $_; last if /^__DATA__$/; } print $fh $file,"\n"; } } __DATA__

Above script, concisely named l (or e.g. lines if that one-letter identifier is already taken) and stored somewhere in any of your private $PATH locations, allows you to e.g. in vi

: r ! l 11-13,42,125-234 somefile

and have the specified lines from somefile read into your current buffer after the line of your cursor.
To do the same with emacs, ask LanX, he knows the proper Ctrl-Shift-Meta-Alt-X encantations to do so.
This code is self-modifying: it places the filename it is invoked upon after the __DATA__ token, so if you want to include more lines of the same file, it suffices to say

: r ! l 1234-1500

For that reason this piece of cr.. code is strictly personal and not suitable to be installed system-wide.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re: extract (a range of) numbered lines from a file (awk)
by hippo (Archbishop) on Sep 20, 2016 at 08:51 UTC
    You could also combine sed and awk (TIMTOWTDI applies):

    sed -e '10,15p;4p;s/.*//' file | awk '!/^$/{print $0}' somefile

    TIMTOWTDI indeed. For starters, this has a small bug/typo which is the omission of the > before "somefile". If you put that it in then it will work but the syntax is rather unweildy. We can simplify it on the awk side by using the /./ construct:

    sed -e '10,15p;4p;s/.*//' file | awk /./ > somefile

    But if that's all you are using awk to do, you may as well just grep.

    sed -e '10,15p;4p;s/.*//' file | grep . > somefile

    Instead, here's an awk expression of the full task:

    awk '(NR >= 10 && NR <= 15); NR == 4' file > somefile

    No doubt a sedhead could rattle off the pure sed version but I'm a bit rusty in that department.

Re: extract (a range of) numbered lines from a file
by choroba (Cardinal) on Sep 20, 2016 at 08:44 UTC
    sed -e '10,15p;4p;s/.*//' file | awk '!/^$/{print $0}' somefile

    somefile shouldn't be there, right?

    Removing lines can be done with d in sed, so you can shorten it to

    sed -e '10,15p;4p;d' | awk ...

    but there's also the -n switch for sed which says "don't print", so we can get

    sed -ne '10,15p;5p' | awk ...

    and we can remove emtpy lines in sed easily with /^$/d which removes the need to call awk:

    sed -ne '/^$/d;10,15p;4p' file

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      somefile shouldn't be there, right?

      Right. And I completely forgot about -n which eliminates the call to awk ;-) So all that's necesary is:

      sed -ne '10,15p;5p' file
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        No, that's not the same. It still outputs the empty lines.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: extract (a range of) numbered lines from a file -- oneliner
by Discipulus (Canon) on Sep 20, 2016 at 07:34 UTC
    sed -e '10,15p;4p;s/.*// file | awk !/^$/{print $0} somefile'

    Sorry but I dont speak urdu.. but I would go with

    # warning windows double quotes perl -nle "BEGIN{map{$w{$_}++}map{/^(.*\d)-(.+)$/?$1..$2:$_}split /,/, +pop @ARGV}print if exists $w{$.}" FILENAME 1,2,4-6,11-13

    ++ for your post

    L*

    PS: the regex is inspired from rosettacode

    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.
Re: extract (a range of) numbered lines from a file -- smarter oneliner 27chars
by Discipulus (Canon) on Sep 21, 2016 at 11:03 UTC
    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.

      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.