head command

Hi All,

How can the head command be used to extract only a particular line.
By default head -n filename displays the first n lines. I want only the nth line.
I couldn't get it from forum search.

Thanks,
Sumesh

You can use sed or awk to get the nth line
For example, to display the 10th line

sed -n "10{p;q;}" file

or

awk -v ln=10 ' NR == ln { print; exit } ' file

Anbu,
Thanks for the reply.

Could u please explain the following part

"10{p;q;}"

Thanks,
Sumesh

"10{p;q;}"

p - print command
q - quit command
If the current line in pattern space is the tenth line of the input file then print that line and quit the sed script.If we wont quit the sed command, it will process the file till its end.

Thanks Anbu. It worked. You are of great help to many.

another way:

head -n filename | tail -1

Yogesh,

I think by the code

head -n filename | tail -1

what you meant is

 head -<Required line number> filename | tail -1

I tried it and it worked!!

Thanks,
Sumesh