How can I get a particular line in a 3 gig text file. All the lines have:
- the same length, and
- are delimited by
\n.
And I need to be able to get any line on demand.
How can this be done? Only one line need be returned.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If all the lines have the same length, the best way by far will be to use
dd(1)and give it a skip parameter.Let the block size be the length of each line (including the newline), then you can do:
The idea is to seek past all the previous lines (
skip=<line_no - 1>) and read a single line (count=1). Because the block size is set to the line length (bs=<line-length>), each block is effectively a single line. Redirect stderr so you don’t get the annoying stats at the end.That should be much more efficient than streaming the lines before the one you want through a program to read all the lines and then throw them away, as
ddwill seek to the position you want in the file and read only one line of data from the file.