To print a specific line from a file

tail -n +<N> <file> | head -n 1
Tail is much faster than sed, awk because it doesn't check for regular expressions.
Sample Output
$ tail -n +5 /var/log/dmesg | head -n 1
[    0.000000] KERNEL supported cpus:

By: qweqq
2011-09-30 08:30:30

3 Alternatives + Submit Alt

What Others Think

What does "much faster mean"? I made a test with 100000 lines file - difference is ~1ms
zolden · 506 weeks and 1 day ago
su root wc -l file 394955588 file sync && echo 1 > /proc/sys/vm/drop_caches time sed -n -e '390955588 p' file /075c6142-c331-4e78-8e5a-4b7b5520edc1.html real 8m25.225s user 1m15.680s sys 0m10.820s sync && echo 1 > /proc/sys/vm/drop_caches time tail -n +390955588 file | head -n 1 /075c6142-c331-4e78-8e5a-4b7b5520edc1.html real 2m34.209s user 0m13.160s sys 0m10.150s With "awk 'FNR=='390955588'" a wrong line was outputted and it took ~11m. The file i tested the posted command with is 21GB. 100k lines is a small file. It is about maybe 10MB in which case the difference is hard to see. Test machine specs: Intel Core i7 920 2x 1TB HDDs in Software Raid 1
qweqq · 503 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: