How do I extract lines from a file using their line number on unix? How do I extract lines from a file using their line number on unix? unix unix

How do I extract lines from a file using their line number on unix?


Something like "sed -n '1p;5p;1010p;20503p'. Execute the command "man sed" for details.

For your second question, I'd transform the input file into a bunch of sed(1) commands to print the lines I wanted.


with awk it's as simple as:

awk 'NR==1 || NR==5 || NR==1010' "file"


@OP, you can do this easier and more efficiently with awk. so for your first question

awk 'NR~/^(1|2|5|1010)$/{print}' file

for 2nd question

awk 'FNR==NR{a[$1];next}(FNR in a){print}' file_with_linenr file