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.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
Thanks
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.
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.
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:
Code:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.