Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a Unix shell, how can I pick a single line from a text file by its line number?

Say I want whatever is at line 3 in animals.txt to be written to stdout (bat bat bat).

monkey monkey monkey
cat cat cat
bat bat bat
horse horse horse

Is there a standard program or simple way to do this?

(There's also the case where the text file does not contain enough lines to contain the line number you ask for)

share|improve this question
up vote 5 down vote accepted

This is one way:

sed -n '3p' file

Here's another:

head -n 3 file | tail -n 1
share|improve this answer
Thanks. Using head/tail like that didn't occur to me, for which I'm a bit ashamed :-) – xyz Sep 27 '10 at 14:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.