21

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.

jgg
  • 1,136
  • 4
  • 22
  • 46

5 Answers5

49

This forum entry suggests:

sed -n '52p' (file)

for printing the 52th line of a file.

schnaader
  • 49,103
  • 10
  • 104
  • 136
  • Why the ()? That gives my bash version an error, might have been corrected in later versions though. – Anders Jul 20 '10 at 00:44
  • 1
    He means to have you replace (file) with the file – Malfist Jul 20 '10 at 01:00
  • 3
    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 Jul 20 '10 at 08:25
  • 1
    @Philipp, Yes, however that won't on all sed versions, I believe that's GNU sed specific. – Anders Jul 20 '10 at 08:32
6

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

Philipp
  • 48,066
  • 12
  • 84
  • 109
thegeek
  • 2,388
  • 2
  • 13
  • 10
  • 1
    what if I want more than one records from 1000 lines of file? how can I pass multiple number of lines in this commnd? – Pooja25 Sep 22 '16 at 21:38
2
command | sed -n '10p'

or

sed -n '10p' file
Anders
  • 6,188
  • 4
  • 26
  • 31
2

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
  • 1,936
  • 1
  • 18
  • 20
1

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
  • 327,991
  • 56
  • 259
  • 343