Quick Unix command to print non-contiguous lines from a text file?
$ awk -v lines="2 4 7" 'index(" "lines" "," "NR" ")' file BobDaphneHeather$ awk -v lines="3 5" 'index(" "lines" "," "NR" ")' file CarlErwin
The blank chars around lines
and NR
in the above are necessary so that NR
value 9
doesn't match when lines
contains 19
, for example.
If you don't mind hard-coding the line numbers inside the script you could alternatively do:
awk 'NR~/^(2|4|7)$/' file