LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash script question (grep and awk) (https://www.linuxquestions.org/questions/linux-software-2/bash-script-question-grep-and-awk-309672/)

hamish 04-04-2005 02:06 PM

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

win32sux 04-04-2005 02:09 PM

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...

hamish 04-04-2005 02:10 PM

it might be possible by grepping by using the ^ character, ie only searching for the expression at the start of the line.

h

jschiwal 04-05-2005 01:05 AM

Using sed or awk, you wouldn't need to use grep if you use a /regular expression/ for the pattern.

hamish 04-05-2005 08:04 AM

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

jiml8 04-05-2005 08:28 AM

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"

jschiwal 04-06-2005 03:14 PM

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.


All times are GMT -5. The time now is 10:15 AM.