Search code examples
unixsedawkline-numberstext-extraction

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


Using sed or similar how would you extract lines from a file? If I wanted lines 1, 5, 1010, 20503 from a file, how would I get these 4 lines?

What if I have a fairly large number of lines I need to extract? If I had a file with 100 lines, each representing a line number that I wanted to extract from another file, how would I do that?


Solution

  • 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.