UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 28th, 2004, 08:03 AM
j2dizzo j2dizzo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 13 j2dizzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
line addressing using grep

Could someone help me by using grep to address specific line number. Assuming line 37 in a shell script has a syntax error and I want to display the line on the terminal using grep, what grep option and/or regular expression should I use? Tried using

grep -n 37 foo.sh

but it didn't work.
Any help will be useful. Thanks

j2dizzo

Reply With Quote
  #2  
Old February 28th, 2004, 08:51 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0


Do you have to use grep?? sed would be my choice:

$ sed -n '2p' <file> => Show second line, ranges are also possibel. 2,5 will list lines 2 to 5.

To my knowledge, you cannot give a range (line numbers) to grep, it start searching the file and shows hits. You can put the line number in front of a hit (-n) and you can limit the amount of hits (-m).

Reply With Quote
  #3  
Old February 28th, 2004, 09:05 AM
j2dizzo j2dizzo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 13 j2dizzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I know sed and awk will do the task easier but I'm required to use grep. Could you ellaborate on what you mention about using a number before a hit (-n) and limiting it with (-m) 'cos I don't understand. Moreover, I looked up on the man document and it doens't mention of the -m option.

Reply With Quote
  #4  
Old February 28th, 2004, 09:20 AM
druuna druuna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 137 druuna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 37 sec
Reputation Power: 0
First things first: Which grep version are you using, and on which OS?

$grep -V
grep (GNU grep) 2.5

The above version, on linux, does have the -m option (also mentioned in the man page).

Ok, the -n option: Show the linenumber of the line that has a hit:

$ grep -n root /etc/passwd
1:root:x:0:0::/root:/bin/bash

The -m option: Only show set amount of hits.

$ grep -m 2 PATH /etc/profile (show only first 2 hits):
export MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man
export PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/local/lib/pkgconfig"

$ grep -n -m 1 PATH /etc/profile (show first hit an put linenumber in front):
7:export MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man

If I understand you problem correctly, you want to show line X (or maybe a range X,Y) and do that using grep.

Like I stated before, this cannot be done using grep by itself. Grep searches complete files (or a variable string).

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > line addressing using grep


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway