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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-04-2005, 02:06 PM
|
#1
|
|
Member
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720
Rep:
|
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
|
|
|
|
04-04-2005, 02:09 PM
|
#2
|
|
Guru
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870
|
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...
|
|
|
|
04-04-2005, 02:10 PM
|
#3
|
|
Member
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720
Original Poster
Rep:
|
it might be possible by grepping by using the ^ character, ie only searching for the expression at the start of the line.
h
|
|
|
|
04-05-2005, 01:05 AM
|
#4
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Using sed or awk, you wouldn't need to use grep if you use a /regular expression/ for the pattern.
|
|
|
|
04-05-2005, 08:04 AM
|
#5
|
|
Member
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720
Original Poster
Rep:
|
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
|
|
|
|
04-05-2005, 08:28 AM
|
#6
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
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"
|
|
|
|
04-06-2005, 03:14 PM
|
#7
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:53 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
|
|