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

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,

Replies are listed 'Best First'.
Re^2: extract (a range of) numbered lines from a file
by shmem (Chancellor) on Sep 21, 2016 at 17:45 UTC
    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,

        Well, mine doesn't, for sure. Transcript:

        qwurx [shmem] ~> perl -le 'print "line $_" for 1..15' >| linenumber.tx +t qwurx [shmem] ~> sed -ne '10,15p;5p' linenumber.txt line 5 line 10 line 11 line 12 line 13 line 14 line 15 qwurx [shmem] ~> which sed /bin/sed qwurx [shmem] ~> dpkg -S `which sed` sed: /bin/sed qwurx [shmem] ~> dpkg -l sed Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/T +rig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===============-============-============-======================== +============ ii sed 4.2.2-4+b1 amd64 The GNU sed stream edito +r

        Show evidence of yours.

        In defence of my oh-so-cool perl snippet -

        • l is less typing than sed -ne
        • on subsequent calls with the same file, I need not specify the file as argument
        • it is the one script which needed a CHECK block

        And that's pretty much all about that. If I were better at typing, two of them three arguments would be quite pointless ;-)

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