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

Display a certain line of a file 1

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

I'd like to display certain lines of a textfile and only that certain line.

e.G.

Textfile
---------
machine number
display type
hardware key
serial number

-> Now if I choose to display the third line the output shall say "hardware key" or if I choose to display the first line the output shall say "machine number".

Problem is (at least as far as I know) that tail is only able to display the first or last line or several last lines, etc. but NOT only one specific line in the middle of the file ...

How could this be done ?

Regards
Thomas
 
Hi

To display line 3 just use [tt]head[/tt] and [tt]tail[/tt] together :
Code:
head -3 /input/file | tail -1
Or you can use an alternative :
Code:
sed -n '3p' /input/file

[gray]# or[/gray]

awk 'NR==3' /input/file

[gray]# or[/gray]

perl -ne 'print if$.==3' /input/file

[gray]# or[/gray]

ruby -ne 'print if $.==3' /input/file
The speed optimized versions of the above alternatives :
Code:
sed -n '3{p;q}' /input/file

[gray]# or[/gray]

awk 'NR==3{print;exit}' /input/file

[gray]# or[/gray]

perl -ne 'if($.==3){print;exit}' /input/file

[gray]# or[/gray]

ruby -ne 'if $.==3:print;exit end' /input/file
Although not elegant, the [tt]head[/tt] & [tt]tail[/tt] solution was still the fastest, when I measured their speed last time.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top