|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
![]() 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). |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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). |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > line addressing using grep |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|