Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I read a specific line (ex, Line #12) in Unix shell? 1

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
I have a huge file (about 25 millions lines). Is this a way to read Line n directly, without looping througth each line of the file?

Any ideas are appreciated.

David
 
Use head and then tail. So to get line 250
Code:
head -250 bigfile | tail -1

Columb Healy
 
Of course you could always use
Code:
awk 'NR=250 {print}' bigfile

Columb Healy
 
Oops
Spot the mistake in the awk example
should have been
Code:
awk 'NR==250{print}' bigfile


Columb Healy
 
sed -n '250p;q' bigfile

this would end faster I believe, without having to read the whole file.

HTH,

p5wizard
 
make that

sed -n '250p;251q' bigfile

or

sed '250q;d' bigfile

google for sed oneliners and take your pick...

HTH,

p5wizard
 
Or a similar optimisation for awk

Code:
awk 'NR==250{print;exit}' bigfile


Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top