1838576 Members
4161 Online
110128 Solutions
New Discussion

Re: Script help

 
SOLVED
Go to solution
Khashru
Valued Contributor

Script help

i am trying to get the mail address from /etc/mail/aliases by

grep mbui /etc/mail/aliases| awk -F : '{print $2}'
and
cat /etc/mail/aliases |grep mbui |cut -d ":" -f2

in both case i have a output like
mina.bui@au.towerlimited.com

There is blank space in front of the mail address.

how can i eliminate this so that i got something like

mina.bui@au.towerlimited.com
18 REPLIES 18
Arunvijai_4
Honored Contributor

Re: Script help

Hi Khashru,

You can use "sed" to remove blank space, such as

# sed '/^$/d'

or

# sed '/./!d'

Add this to your script and check ..

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Khashru
Valued Contributor

Re: Script help

Where should i put that

I am using the following lines

z=`cat /etc/mail/aliases |grep mbui |cut -d ":" -f2`
echo "User mail address is : $z"
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Script help

Here

=`cat /etc/mail/aliases |grep mbui |cut -d ":" -f2 | sed ...`

By the way where is your blank space?
Vibhor Kumar Agarwal
Arunvijai_4
Honored Contributor

Re: Script help

Hi Khashru,

You can use perl to this.. I got this working, just store it in a file,

# cat /etc/mail/aliases |grep mbui |cut -d ":" -f2 > test

# perl -ni.bak -e 'chop; s/^\s//; print "$_\n";' test

# cat test

should remove first blank

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
H.Merijn Brand (procura
Honored Contributor

Re: Script help

# perl -nle'/mbui/&&print((split/\s*:\s*/)[1])' /etc/mail/aliases

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Khashru
Valued Contributor

Re: Script help

I cannot use perl. i have tried with both option but no output

hpprod:/home/mayub/script :cat /etc/mail/aliases |grep mbui |cut -d ":" -f2 |sed '/^$/d'
mina.bui@au.towerlimited.com
hpprod:/home/mayub/script :cat /etc/mail/aliases |grep mbui |cut -d ":" -f2 |sed '/./!d'
mina.bui@au.towerlimited.com


Please suggest me the exact command.
A. Clay Stephenson
Acclaimed Contributor

Re: Script help

It's usually less than smart to use several commands when 1 will work but it you want to use grep and awk then here's the key: the shell's read command

grep "mbui" < /etc/mail/aliases | awk 'F ':' '{print $2}' | read U
echo "${U}"
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Script help

Hi Khashru,

>>> Where should i put that
z=`cat /etc/mail/aliases |grep mbui |cut -d ":" -f2`
echo "User mail address is : $z" <<<

In response to your above question try the following...

z=`cat /etc/mail/aliases |grep mbui |cut -d ":" -f2 | tr -d ''`
echo "User mail address is : $z"
Khashru
Valued Contributor

Re: Script help

Clay,

Nothing comes with that command

hpprod:/home/mayub/script :grep "mbui" /etc/mail/aliases | awk 'F ':' '{print $2}' | read U
> echo "${U}"
>
Khashru
Valued Contributor

Re: Script help

Hi sandman,

Nothing happens. i got the same output. space in front of the mail address
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Script help

Ooops, there is an extra quote in this command so 'F should be -F.

grep "mbui" < /etc/mail/aliases | awk 'F ':' '{print $2}' | read U
echo "${U}"

should be:
grep "mbui" < /etc/mail/aliases | awk -F ':' '{print $2}' | read U
echo "${U}"
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Script help

Hi Khashru,

Try the awk construct below...this should certainly solve it:

# grep mbui /etc/mail/aliases | awk -F": " '{print $2}'

Note the field separator in awk is two characters ": " i.e. a colon followed by a space enclosed in double-quotes.

hope it helps!
Khashru
Valued Contributor

Re: Script help

Thanks Clay. It worked.
Sandman!
Honored Contributor

Re: Script help

Khashru,

Here's an alternative to the command that you have been using...

# grep mbui /etc/mail/aliases | cut -d" " -f2

Simply replacing the field-delimiter from a colon to a space will do the trick. Goes also for the awk solution I gave you.

# grep mbui /etc/mail/aliases | awk -F" " '{print $2}'

cheers!
A. Clay Stephenson
Acclaimed Contributor

Re: Script help

I should point out that your solution is rather naive in that alias entries can continue across lines and may contain many comma separated values so that simply outputting $2 is far from robust. If you know that you aliases are very simple and will remain so then OK but you would get at best a grade of "D+" from me for any of your solutions.
If it ain't broke, I can fix that.
Khashru
Valued Contributor

Re: Script help

Hi Clay,

Till now my aliases file is very simple but may be in future i have comma separated value. In that case what should i do to overcome that?
James R. Ferguson
Acclaimed Contributor

Re: Script help

Hi Khashru:

Continuing in Merijn's vain (using Perl) and assuming that you want to parse comma-separated aliases:

# perl -nle 'if (/mbui/) {foreach $name (map {split /,/} ((split/\s*:\s*/)[1])) {print $name}}' /etc/mail/aliases

This would take entries like:

operator : root,khashru,whomever

...and output:

root
khashru
whomever

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Script help

Hi (again) Khashru:

I'm sorry, I changed the example as I wrote it. If you wanted to match "oper" as in "operator" in the aliases file:

# perl -nle 'if (/oper/) {foreach $name (map {split /,/} ((split/\s*:\s*/)[1])) {print $name}}' /etc/mail/aliases

This would take entries like:

operator : root,khashru,whomever

...and output:

root
khashru
whomever

Thus, simply change the token you want to match as needed.

Regards!

...JRF...