Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-28-2004, 08:08 AM
|
#1
|
LQ Newbie
Registered: Feb 2004
Posts: 15
Rep:
|
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 foo.sh
but it didn't work.
Any help will be useful. Thanks
j2dizzo
|
|
|
02-28-2004, 08:16 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
02-28-2004, 09:08 AM
|
#3
|
LQ Newbie
Registered: Feb 2004
Posts: 15
Original Poster
Rep:
|
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.
|
|
|
02-28-2004, 09:21 AM
|
#4
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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).
|
|
|
02-28-2004, 10:23 AM
|
#5
|
LQ Newbie
Registered: Feb 2004
Posts: 15
Original Poster
Rep:
|
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
|
|
|
02-28-2004, 12:05 PM
|
#6
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
Last edited by anon237; 02-28-2004 at 12:06 PM.
|
|
|
02-28-2004, 12:19 PM
|
#7
|
Member
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60
Rep:
|
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! 
|
|
|
02-28-2004, 12:25 PM
|
#8
|
Member
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60
Rep:
|
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.

|
|
|
02-28-2004, 12:57 PM
|
#9
|
LQ Newbie
Registered: Feb 2004
Posts: 15
Original Poster
Rep:
|
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.
j2dizzo
|
|
|
02-28-2004, 01:18 PM
|
#10
|
Member
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60
Rep:
|
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!

|
|
|
02-28-2004, 01:28 PM
|
#11
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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 
|
|
|
02-28-2004, 01:45 PM
|
#12
|
Member
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60
Rep:
|
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! 
|
|
|
02-28-2004, 03:16 PM
|
#13
|
LQ Newbie
Registered: Feb 2004
Posts: 15
Original Poster
Rep:
|
Well, thanx for your help.
j2dizzo
|
|
|
03-03-2004, 09:36 AM
|
#14
|
Member
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60
Rep:
|
The answer (well, an answer)
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.

Last edited by dford; 03-03-2004 at 09:37 AM.
|
|
|
All times are GMT -5. The time now is 01:46 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|