tags:

views:

60

answers:

5

I am looking for a unix command to get a single line by passing line number to a big file (with around 5 million records). For example to get 10th line, I want to do something like

command file-name 10

Is there any such command available? We can do this by looping through each record but that will be time consuming process.

+1  A: 
command | sed -n '10p'

or

sed -n '10p' file
Anders
Thanks it works!
goutham
+1  A: 

This forum entry suggests:

sed -n '52p' (file)

for printing the 52th line of a file.

schnaader
Thanks it works!
goutham
Why the ()? That gives my bash version an error, might have been corrected in later versions though.
Anders
He means to have you replace (file) with the file
Malfist
I was able to speed this up by a factor of ten (when used with a file with 100000 lines) by quitting after printing: `sed -n '52{p;q}'`
Philipp
@Philipp, Yes, however that won't on all sed versions, I believe that's GNU sed specific.
Anders
+1  A: 

You could do something like:

head -n<lineno> <file> | tail -n1

That would give you the <lineno> lines, then only give the last line of output (your line).

Edit: It seems all the solutions here are pretty slow. However, by definition you'll have to iterate through all the records since the operating system has no way to parse line-oriented files since files are byte-oriented. (In some sense, all these programs are going to do is count the number of \n or \r characters.) In lieu of a great answer, I'll also present the timings on my system of several of these commands!

[mjschultz@mawdryn ~]$ time sed -n '145430980p' br.txt
0b10010011111111010001101111010111

real    0m25.871s
user    0m17.315s
sys 0m2.360s
[mjschultz@mawdryn ~]$ time head -n 145430980 br.txt | tail -n1
0b10010011111111010001101111010111

real    0m41.112s
user    0m39.385s
sys 0m4.291s
[mjschultz@mawdryn ~]$ time awk 'NR==145430980{print;exit}' br.txt 
0b10010011111111010001101111010111

real    2m8.835s
user    1m38.076s
sys 0m3.337s

So, on my system, it looks like the sed -n '<lineno>p' <file> solution is fastest!

mjschultz
A: 

you can use awk

awk 'NR==10{print;exit}' file

Put an exit after printing the 10th line so that awk won't process the 5 million records file further.

ghostdog74
A: 

Going forward, There are a lot of ways to do it, and other related stuffs.

If you want multiple lines to be printed,

sed -n -e 'Np' -e 'Mp'

Where N and M are lines which will only be printed. Refer this 10 Awesome Examples for Viewing Huge Log Files in Unix

thegeek