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

command to output a specific line in a file 1

Status
Not open for further replies.

yahoo182

Programmer
Jul 5, 2005
70
CA
Hi there,
I know in Unix, the head command can be used to show the first few lines of a file. However, what if we want to show only a specific line of it. Say if we wanted to show line 5 of a file named test.txt, is there any command that does this?

thanks!
 
One way:
sed -n '5p' test.txt
Another way:
awk 'NR==5{print;exit}' test.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV.
That was just the command I needed! :)
 
Hi there, i'm tryng to write a script for this command.
Do you know how I can embed the $1 argument into the sed command?
My command will be something like:

showline.sh 3 test.txt

!/bin/sh
line=$1
file=$2

sed -n '"$line"p' $2
 
sed -n "${line}p" $file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top