Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
$ 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).
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.
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?
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.
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.
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!
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
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.