Operating System - HP-UX
1833045 Members
2598 Online
110049 Solutions
New Discussion

display a specific line from a file

 
SOLVED
Go to solution
mooi-kuan
Advisor

display a specific line from a file

Hi,
I need to read a specific line from an input file. Is there any unix command syntax like,

unixcmd 4 file_name

where 4 is the line number to be read.

Thank you.
mk
10 REPLIES 10
mooi-kuan
Advisor

Re: display a specific line from a file

Sorry, I found a way for this using
sed -n 4p file_name

but if you have any other suggestion, please let me know too. Thank you.
mk
Madhu Sudhan_1
Respected Contributor

Re: display a specific line from a file

Mooi-kuan lim:

One of the other way to do the same is:

# awk -v line=5 'NR == line { print $0 }' /etc/passwd

You can replace 5 with any shell variable if you need to. Here I have taken the file /etc/passwd.

Enjoy !
......Madhu

Think Positive
federico_3
Honored Contributor

Re: display a specific line from a file

type simply ( if you want to get the line 5 ):

cat file | awk '{ if (NR==5) {print $0}}'


regards,

federico
Stefan Schulz
Honored Contributor

Re: display a specific line from a file

Using sed is the fastest solution to this porblem. Of course you can also use awk or perl or .....

But if you have to select a known line by number sed is the fastest.
No Mouse found. System halted. Press Mousebutton to continue.
Stefan Schulz
Honored Contributor
Solution

Re: display a specific line from a file

Using sed is the fastest solution to this porblem. Of course you can also use awk or perl or .....

But if you have to select a known line by number sed is the fastest.
No Mouse found. System halted. Press Mousebutton to continue.
Bruce Regittko_1
Esteemed Contributor

Re: display a specific line from a file

...And just to be different:

head -X | tail -1

where X is the line number to be displayed.

Of course though, sed is the way to go.
www.stratech.com/training
Dan Hetzel
Honored Contributor

Re: display a specific line from a file

Hi,

There are many ways to achieve what you want, including a for or while loop, but 'sed' is definitely the way to go.

sed -n ${LINENUM}p < filename

even allows you to have your line number in a variable

sed -n ${FIRST},${LAST}p < filename
will print lines from FIRST to LAST

There should be a space after the '-n' and no space before 'p'

Dan


Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Igot ToMuch Time
New Member

Re: display a specific line from a file

Try this:

ex junk.txt << EOF
5p
EOF
Ivan Ferreira
Honored Contributor

Re: display a specific line from a file

This will print the 4th line:

awk 'NR==4 {print $0}' test
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor

Re: display a specific line from a file

You can create an script called showline like this:

showline.sh:

awk -v LINE=$1 'NR==LINE {print $0}' $2

Usage:

showline.sh
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?