LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-04-2005, 02:06 PM   #1
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Rep: Reputation: 30
Bash script question (grep and awk)


Hello

I'm trying to get a script working, but I'm stuck

My script is set up to email a group of people. the email addresses are stored in a file looking like:

0 Mark x@aastudios.co.uk
1 Tim y@agtranslations.co.uk
2 Alan z@breathemail.net
3 Alan a@acdesign.freeserve.co.uk
4 Gary bu2004@akrepro.co.uk

Thus, the email script greps for the id number (0 to 4) and them awks for the email address.

However, sometimes the grep command will get multiple lines, for example, grepping for "0" will return:
0 Mark x@aastudios.co.uk
4 Gary bu2004@akrepro.co.uk

this will then not work for the emailing because the awk will get two lines instead of just one.

Let us assume that we are in the situation as above, where I have to send an email to id number 4, Gary, however, my grep has given me two lines.

How can I seperate line id ="4"?

Many thanks
Hamish
 
Old 04-04-2005, 02:09 PM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
maybe, instead of "grep 4", you could do a "grep ^4" or something similar...

the "^" means (in regex) "starting with"... this way the 4 later in the line won't matter...

just my two cents...
 
Old 04-04-2005, 02:10 PM   #3
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
it might be possible by grepping by using the ^ character, ie only searching for the expression at the start of the line.

h
 
Old 04-05-2005, 01:05 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using sed or awk, you wouldn't need to use grep if you use a /regular expression/ for the pattern.
 
Old 04-05-2005, 08:04 AM   #5
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
great. thanks.

do you know it it is possible to pass arguements in the command line which the bash script will handle ?

for example:
$ ./my_script.sh arg=4

and then the script would put the variable $arg equal to four when it ran the script?

is this possible, and are there any good sites?

hamish
 
Old 04-05-2005, 08:28 AM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
try grep -w.

Alternatively, put some character in front of the user number that is not valid for an email address (such as a #, for instance) then test for "#0" instead of testing for merely "0"
 
Old 04-06-2005, 03:14 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You would probably just use '4' as the argument for $2. Unless what you are saying is that you want to have arbitrary argument/value pairs as arguments. If that were the case, you would need to either analyse the argument $1 to determine the argument and value values, or precede the command with variable=value.

If you use:
variable=value command arguments
then you are setting the value of $variable before executing the command. This is commonly done with ./configure scripts or make commands to change defaults. You might want to read the 'info bash' pages at variable expansion. For example you might use something like: ${PARAMETER:+WORD}, or have a test for the value before setting a default.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
bash script and grep syros Programming 4 01-13-2005 03:04 PM
newbe bash question ( grep processing) therealbxp Programming 2 11-20-2004 07:40 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:35 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration