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

how can I print a specific line number in a file??? 5

Status
Not open for further replies.

RobJordan

MIS
Joined
Apr 3, 2001
Messages
296
Location
US
I am trying to AVOID doing a cat of the file
and counting the line numbers until
I find the number I want.

Is there a way to directly print
the line number I want to from a file?

Thanks in advance,

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
 
grep -n &quot;line #:&quot; <file>
Be sure to include the &quot;:&quot;.

Example: grep -n 100: /etc/somefile

Bill.
 
Thanks for your repsone.
I tried this test but I couldn't get it to work.
Maybe it's because I'm using hpux?

# cat /tmp/TMP_LIST.258.menu
Validate
array.fun
check_script
check_script.bak
find_term
findit
findit.bak
findit.beta
index.html
lsit
menu.fun
menu.fun.bak
portman
portman.beta.091701
root
test
trapit.fun
tuxtool
tuxtool.010202
tuxtool.array.bak
tuxtool.bak.gz
validate.fun
yesterday.fun

# grep -n 5: /tmp/TMP_LIST.258.menu
root@awhq6394 [/tmp]
#

the grep statement returned nothing Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
 
Check the man pages (or perhaps remove the &quot;:&quot;).

grep has an option to display line numbers.

Bill.
 
You could use awk as follows
Code:
awk &quot;NR==2&quot; file
Or to exit after printing the line for greater efficiency
Code:
awk &quot;NR==2{print;exit}&quot; file
Hope this helps. CaKiwi
 
Didn't work for me under NetBSD either...

Getting closer with 'cat -n <filename> | grep -w #' but it still matches any other 'word instance' of #...

Matt
matt@paperlove.org
If I can help, I will.
 
Ah! I knew there would be a way with awk... Alas, awk is still on my lengthy list of things to learn... :(

That works for me, CaKiwi... Matt
matt@paperlove.org
If I can help, I will.
 
It is well worth spending a little time to learn. It is not too difficult if you already understand regular expressions. Check the awk forum here on TT for many script examples. CaKiwi
 
To get line 9 try:
sed -n &quot;9p&quot; <file_name>

Steve.
 
It appears the head|tail command
is the fastest.

Robert

# ./speed_test
--------------------------------------------------
timex awk &quot;NR==400{print;exit}&quot; /tmp/tmp.out
srwxrwxrwx 1 root sys 0 Mar 6 10:10 .cgistub_6567

real 0.04
user 0.02
sys 0.02

--------------------------------------------------
timex head -400 /tmp/tmp.out | tail -1

real 0.02
user 0.01
sys 0.01

srwxrwxrwx 1 root sys 0 Mar 6 10:10 .cgistub_6567
--------------------------------------------------
timex sed -n 400p /tmp/tmp.out
srwxrwxrwx 1 root sys 0 Mar 6 10:10 .cgistub_6567

real 0.03
user 0.01
sys 0.01
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
 
If speed is in need then the best seems to have tail before head as in -

tail +400 /tmp/tmp.out | head -1

Also if one sticks with sed then don't forget to quit on the line printed to save on all the additional lines in the stream. Also

sed '400!d;400q' /tmp/tmp.out

beats out

sed -n '400p;400q' /tmp/tmp.out

Here are some times:

timex head -2666 foo | tail -1

real 0.02
user 0.01
sys 0.01

timex tail +2666 foo | head -1

real 0.01
user 0.00
sys 0.01

timex sed -n '2666p' foo

real 0.02
user 0.01
sys 0.01

timex sed -n '2666p;2666q' foo

real 0.02
user 0.01
sys 0.01

timex sed '2666\!d;2666q' foo

real 0.01
user 0.01
sys 0.01


Cheers,
ND
 
Hi Guys

Check this out!!
I guess it will work in all flavours of UNIX

FILE_NAME=$1
LINE=$2
grep -n '' $FILE_NAME|awk ' BEGIN{ FS=&quot;:&quot;}{if ($1==LINE_NO) print $0}' LINE_NO=$LINE|cut -d : -f 2

Copy the above code in script_file and make it executable.
Then run it by providing the_target_file_name and line_no as parameter...e.g.
script_file the_target_file_name line_no

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top