Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: how to extract thousands of non-sequential lines from a file

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?

like image 672
dlv Avatar asked Jan 26 '26 05:01

dlv


1 Answers

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

Test:

$ 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
like image 91
jaypal singh Avatar answered Jan 28 '26 20:01

jaypal singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!