I have a file with millions of lines. I also have a list of line numbers, a few hundred thousand which are non-sequential and without a pattern. I would like to extract the lines of the file with the list of line numbers. How can I achieve this efficiently in linux?
If you have the list of linenumbers in a file then with awk you can do something like this -
awk 'NR==FNR { a[$1];next } (FNR in a)' line_number_list.txt bigfile.txt
$ cat bigfile.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
$ cat line_number_list.txt
1
2
4
5
8
9
$ awk 'NR==FNR { a[$1];next } (FNR in a)' line_number_list.txt bigfile.txt
line1
line2
line4
line5
line8
line9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With