Re: shell script (121 Views)
Reply
Occasional Advisor
mougli
Posts: 7
Registered: ‎07-18-2006
Message 1 of 8 (121 Views)
Accepted Solution

shell script

Hi,
I have a string like
"ax-a1: xxx@xxx.com yyy@yyy.com zzz@zzz.com;
ax-a2: aaa@aaa.com bbb@bbb.com ccc@ccc.com;"

I need to extract the mailid seperated by a space for any given user like ax-a1.

I have tried to use
Test=`grep a1-ax mailids.txt`
echo $Test | awk '{print substr($0,":",index($0,";"))}'

but it doesn't seem to work. Can you please suggest??

TIA
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,581
Registered: ‎05-19-2003
Message 2 of 8 (121 Views)

Re: shell script


Hmm,

The second argument to substr is an offset numner, and you passed it a piece of string.
That's not goign to work.

Please explain a little more clearly what you expact for output and where the input comes from.
- A single long string with multiple usernames + email?
- When ax-a1 is select, which are we supposed to return: xxx@xxx.com, or all of "xxx@xxx.com yyy@yyy.com zzz@zzz.com"?


You may readily find a solution by using a regexpr for field seperator. Check out the following example:


$ cat x
ax-a1: xxx@xxx.com yyy@yyy.com zzz@zzz.com; ax-a2: aaa@aaa.com bbb@bbb.com ccc@ccc.com;
$ awk -F'[:; ]+' '{print $3}' x
yyy@yyy.com
$ awk -F'[:;]' '{print $3}' x
ax-a2
$ awk -F'[:;]' '{print $2}' x
xxx@xxx.com yyy@yyy.com zzz@zzz.com


Hope this helps some...
Hein.

Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 3 of 8 (121 Views)

Re: shell script

Hi,

if your string has an entry
ax-a1
und you grep for
a1-ax
you won't get results :-)

This will give you the second field stripped by the last character, which is ";" in your case:

awk -F: -v user=ax-a1 '$1 == user {print substr($2,1,length($2)-1)}' mailids.txt

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,581
Registered: ‎05-19-2003
Message 4 of 8 (121 Views)

Re: shell script

Buildin up on the external passed usename you could 'walk' the field in steps of 2 looking for a username match.
Note, I had to strip spaces from the username for it to match easily:

$awk -F'[:;]' -v user=ax-a2 '{i=0; while (i++
aaa@aaa.com bbb@bbb.com ccc@ccc.com


Or you could have 'fields' be user:email by using ';' as FS (field seperator) and then splitting on ":" to look at the parts:

$ awk -F\; -v user=ax-a1 '{i=0; while (i++ xxx@xxx.com yyy@yyy.com zzz@zzz.com


Hein.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 8 (121 Views)

Re: shell script

Hi:

How about:

# STR="ax-a1: xxx@xxx.com yyy@yyy.com zzz@zzz.com;ax-a2: aaa@aaa.com bbb@bbb.com ccc@ccc.com;"

# echo ${STR} | perl -nle 'BEGIN{$u=shift};print $1 if m/$u:(.+?);/' ax-a1

...would return:

xxx@xxx.com yyy@yyy.com zzz@zzz.com

Regards!

...JRF...
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 6 of 8 (121 Views)

Re: shell script

Here's a way of doing it with sed:

# echo $Test | sed -n 's/\(.*\): \(.*\);/\2/p'

...and with awk

echo $Test | awk -F": " '{print z[split($2,z,";")-1]}'
Please use plain text.
Honored Contributor
dirk dierickx
Posts: 958
Registered: ‎12-20-1999
Message 7 of 8 (121 Views)

Re: shell script

cut -d":" -f 2

you'll still have a ';' at the end but that's removed quickly with sed or something similar.
Please use plain text.
Occasional Advisor
mougli
Posts: 7
Registered: ‎07-18-2006
Message 8 of 8 (121 Views)

Re: shell script

Thanks everyone. Got it working now.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation