LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
Search this Thread
Old 02-28-2004, 09:08 AM   #1
j2dizzo
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Rep: Reputation: 0
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
 
Old 02-28-2004, 09:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 02-28-2004, 10:08 AM   #3
j2dizzo
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
Old 02-28-2004, 10:21 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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).
 
Old 02-28-2004, 11:23 AM   #5
j2dizzo
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 02-28-2004, 01:05 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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 druuna; 02-28-2004 at 01:06 PM.
 
Old 02-28-2004, 01:19 PM   #7
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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!
 
Old 02-28-2004, 01:25 PM   #8
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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.

 
Old 02-28-2004, 01:57 PM   #9
j2dizzo
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 02-28-2004, 02:18 PM   #10
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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!

 
Old 02-28-2004, 02:28 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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
 
Old 02-28-2004, 02:45 PM   #12
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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!
 
Old 02-28-2004, 04:16 PM   #13
j2dizzo
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Original Poster
Rep: Reputation: 0
Well, thanx for your help.

j2dizzo
 
Old 03-03-2004, 10:36 AM   #14
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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 10:37 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
command line options for firefox for shell scripting. dr_zayus69 Programming 1 05-25-2005 12:36 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 03:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration