-   Linux - General (
-   -   line addressing with grep (shell scripting) (

j2dizzo 02-28-2004 09:08 AM

line addressing with grep (shell scripting)
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

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


druuna 02-28-2004 09:16 AM

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).

Hope this helps.

j2dizzo 02-28-2004 10:08 AM

Thanks druuna for your reply you made on the two posts. I know sed and awk will do the task easier but I'm required to use grep. Could you elaborate 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.

druuna 02-28-2004 10:21 AM

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

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).

j2dizzo 02-28-2004 11:23 AM

Ok, I'm using my SunOS 5.6 and that's why the -m ption is not provided in the man page. Anyways, you are right about my question which I want to show line X (or maybe a range X,Y) and do that using grep. I understand that this can't be done by grep itself, so is possible that it has to be piped to another filter?

druuna 02-28-2004 01:05 PM

Yes you could, but what would be the point?

You already seem to know the line(number) that holds the error, only thing you want to do is show that specific line.

This is what I think you want:
sed -n '2p' <file> | grep <the_error>
and it doesn't make too much sense. Grep alone would have shown you that line, sed is not needed. Or sed shows you the line, grep is not needed.

Finding the error in the first place would be a job that grep could perform and, depending on options set, show the linenumber and/or the line with the error itself even a few lines before and/orf after the line with the error.

I'm starting to wonder if this is just a small part of a bigger problem. Your subject does mention shell scripting.

dford 02-28-2004 01:19 PM

Is this a school assignment?

I assume yes. Think about what patterns you would need to do a grep of a grep and get the right output. Read the man page for grep. Use the "-n" option that was mentioned. It is really pretty easy and that is enough hinting for the moment.

Have fun with it! :D

dford 02-28-2004 01:25 PM

By the way, I probably wouldn't do grep either for this one maybe something like:
head -37 foo.cpp | tail -1

Unix/Linux is wonderful for the myriad of different ways you can combine the tools.


j2dizzo 02-28-2004 01:57 PM

Yes dford, it's a school assignment and I'm required to use grep, sed, and awk to perform the task. I've been able to do sed and awk but grep seems to be difficult.

druuna: Yes, this is just a small part of a bigger problem. I've been able to complete the others but I just need grep to do this and maybe the output should be piped to another filter.

Thanks all for your help, I'll keep trying other methods of using grep.


dford 02-28-2004 02:18 PM

It is always important to start out by stating your problem in a clear ways. (Requirements)

You wish to select a line from a file.
The only information you know about the line is its line number: 37 in this case.
The only tool you are allowed to use is grep.

What options does grep have that relate to line number? (read the man page, although it has already been mentioned twice.)

How do I use that option and other capabilities of grep to select the line?

Like I say have fun with this. Play with the grep options. Try to do pieces of the puzzle and then combine them together. In Unix there is very seldom only one way to do something even using the same tool or tools!


druuna 02-28-2004 02:28 PM

Seems a bit odd to me that you have to solve this using only grep. It cannot be done.

Most teachers don't have the sense of humor to give 'trick' questions. Are you reading the assignment correctly? Could it be that you have to solve 1 assignment (maybe more) using 1 or more of the three tools (sed, awk and grep) given?

If I'm wrong then I would like to hear the answer myself :)

dford 02-28-2004 02:45 PM

I'll post the answer next week sometime. Or j2dizzo can post it when he gets it. Although his class mates might be reading this! :D

j2dizzo 02-28-2004 04:16 PM

Well, thanx for your help.


dford 03-03-2004 10:36 AM

The answer (well, an answer)
Okay, here is my answer to the problem of selecting a specific line from a file using only grep:


grep -n '' base_it.C | grep '^37:'
How this works is the first grep selects everything in the file using an empty regex and the "-n" option displays the line numbers of the selected lines in the output. The second grep simply looks for a 37: at the beginning of the line (^). Not too hard, but something where you have to think about your options and the output.


All times are GMT -5. The time now is 05:02 AM.