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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing a particular line 1

Status
Not open for further replies.

Ambatim

Programmer
Feb 28, 2002
110
IN
Is there any command in UNIX to print a particular line from a file.
I want to print only the second line from a file.

Any ideas ???

Thanks in Advance
Mallik
 
Ambatim:

how about using awk?

awk ' { if (NR == 2)
print $0 } ' < filename.txt

Regards,

Ed
Schaefer
 
there is always my favorite head | tail combination:
head -2 <filename> | tail -1

Although this is two commands, they seem to run faster than awk or sed. But any of these options should work for you. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
&quot;sed -n 2p file&quot; ..... neat!
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
If you have a huge number of lines then better would be

sed -n '2p;2q' file

or (the same)

sed '2!d;2q' file

The 2q command tells sed to quit on that line, after the print command, so that the rest of the input is ignored.

Cheers,
ND [smile]
 
Ok -- now don't think I'm being picky here (I probably am, but stil)

How would you print a range of lines using sed?
Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike, I've just been messing about with another thread here (printing the &quot;context&quot; of a grep result) and it seems, for example if you wanted to print lines 5-10, you would do sed -n 5,10p filename

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top