Operating System - HP-UX
1829894 Members
2294 Online
109993 Solutions
New Discussion

Retrieving a particular line from a file

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Retrieving a particular line from a file

Hi,

Is there a UNIX utility which allows me to retrieve/display a particular line from a file?

I'm trying to retrieve the 2nd line from a text file which consists of several other lines as in:
ken_lee
plnge12
7219
200
Ken Lee
/home/cs/pub
/bin/tcsh

I had tried using `head -2 myFile` but it displayed both lines 1 and 2 of the file "myFile".

I have another method by using PERL to read in the contents of the file into an array an output the proper elements i.e:

#!/usr/intel/bin/perl
open(INPUTFILE, "myFile");
@myArray = ;
close(INPUTFILE);
$passwd = $myArray[1];
print "\$passwd = $passwd \n";

However, I was wondering if there are any other methods to solving this problem?

Note that there are many other files of this form having entirely different entries. Therefore, I need a way to obtain the 2nd line in the file, or even better, be able to specify any line number and display the contents at that line number.

Could anyone please help me out?

Thanks
10 REPLIES 10
Yogeeraj_1
Honored Contributor
Solution

Re: Retrieving a particular line from a file

hi,

try to pipe it through a "tail -1". This should do the trick!
head -2 |tail -1


E.g.
K250: home/yd>head -2 /etc/passwd
root:EGVtdOx8xnUI.:0:3::/:/sbin/sh
daemon:*:1:5::/:/sbin/sh
K250: home/yd>head -2 /etc/passwd|tail -1
daemon:*:1:5::/:/sbin/sh
K250: app/cmtpay>


Hope this helps!
Regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Tom Geudens
Honored Contributor

Re: Retrieving a particular line from a file

Hi,
Try piping it through an awk. Example
cat /etc/copyright | awk '(NR == 2)'

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
Jean-Louis Phelix
Honored Contributor

Re: Retrieving a particular line from a file

hi,

Another simple way to do it is awk ...

# awk 'NR == 3' file

will print third line of file

Regards.
It works for me (© Bill McNAMARA ...)
Systeemingenieurs Infoc
Valued Contributor

Re: Retrieving a particular line from a file

i normally use the head/tail. An alternative to get the third line :

nl yourfile|grep "^ *3 *"

A Life ? Cool ! Where can I download one of those from ?
Sridhar Bhaskarla
Honored Contributor

Re: Retrieving a particular line from a file

Hi,

You can use this simple and silly script. Call it getline.ksh

#!/usr/bin/ksh
if [ $# -ne 2 ]
then
echo "Usage: $0 filename line_number"
exit
fi
FILE=$1
LINE=$2
head -${LINE} ${FILE} |tail -1


$./getline.ksh /etc/services 14

Will print 14th line of /etc/inetd.conf.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Carlos Fernandez Riera
Honored Contributor

Re: Retrieving a particular line from a file

X=5
Y=7

sed -n -e ${X},${Y}p file
unsupported
Sean OB_1
Honored Contributor

Re: Retrieving a particular line from a file

How about:

head -2 filename | tail -1


John Meissner
Esteemed Contributor

Re: Retrieving a particular line from a file

easier than everyone else's....
try this

sed -n "2p" filename

sed -n followed by the number of the line you want and "p" to indicate you want to print to stdout
All paths lead to destiny
H.Merijn Brand (procura
Honored Contributor

Re: Retrieving a particular line from a file

Since you already tried perl, here's some perl examples:

Show line 23
# perl -ne'23..23 and print' file

show lines 432 through 5546
# perl -ne'432..5546 and print' file

show from line 2 upto and including a line with "foo"
# perl -ne'2../foo/ and print' file

show line containing foo and the next line
# perl -ne'/foo/ or next; print $_,scalar<>;last' file
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Retrieving a particular line from a file

This member has assigned points to 280 of 406 responses to his/her questions.

Now that's lazy!

Lokk at your profile and find previous posts where you haven't assigned points!

live free or die
harry
Live Free or Die