1860653 Members
4375 Online
110414 Solutions
New Discussion

Re: shell script

 
SOLVED
Go to solution
mougli
Occasional Advisor

shell script

Hi,
I have a string like
"ax-a1: [email protected] [email protected] [email protected];
ax-a2: [email protected] [email protected] [email protected];"

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
7 REPLIES 7
Hein van den Heuvel
Honored Contributor
Solution

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: [email protected], or all of "[email protected] [email protected] [email protected]"?


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


$ cat x
ax-a1: [email protected] [email protected] [email protected]; ax-a2: [email protected] [email protected] [email protected];
$ awk -F'[:; ]+' '{print $3}' x
[email protected]
$ awk -F'[:;]' '{print $3}' x
ax-a2
$ awk -F'[:;]' '{print $2}' x
[email protected] [email protected] [email protected]


Hope this helps some...
Hein.

Peter Nikitka
Honored Contributor

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"
Hein van den Heuvel
Honored Contributor

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++
[email protected] [email protected] [email protected]


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++ [email protected] [email protected] [email protected]


Hein.
James R. Ferguson
Acclaimed Contributor

Re: shell script

Hi:

How about:

# STR="ax-a1: [email protected] [email protected] [email protected];ax-a2: [email protected] [email protected] [email protected];"

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

...would return:

[email protected] [email protected] [email protected]

Regards!

...JRF...
Sandman!
Honored Contributor

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]}'
dirk dierickx
Honored Contributor

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.
mougli
Occasional Advisor

Re: shell script

Thanks everyone. Got it working now.