- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Retrieving a particular line from a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 02:11 AM
12-26-2002 02:11 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 02:17 AM
12-26-2002 02:17 AM
Solutiontry to pipe it through a "tail -1". This should do the trick!
head -2
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 02:48 AM
12-26-2002 02:48 AM
Re: Retrieving a particular line from a file
Try piping it through an awk. Example
cat /etc/copyright | awk '(NR == 2)'
Regards,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 02:50 AM
12-26-2002 02:50 AM
Re: Retrieving a particular line from a file
Another simple way to do it is awk ...
# awk 'NR == 3' file
will print third line of file
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 04:14 AM
12-26-2002 04:14 AM
Re: Retrieving a particular line from a file
nl yourfile|grep "^ *3 *"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 06:56 AM
12-26-2002 06:56 AM
Re: Retrieving a particular line from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 07:01 AM
12-26-2002 07:01 AM
Re: Retrieving a particular line from a file
Y=7
sed -n -e ${X},${Y}p file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 07:38 AM
12-26-2002 07:38 AM
Re: Retrieving a particular line from a file
head -2 filename | tail -1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2002 04:33 AM
12-31-2002 04:33 AM
Re: Retrieving a particular line from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2002 04:41 AM
12-31-2002 04:41 AM
Re: Retrieving a particular line from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2002 04:47 AM
12-31-2002 04:47 AM
Re: Retrieving a particular line from a file
Now that's lazy!
Lokk at your profile and find previous posts where you haven't assigned points!
live free or die
harry