AruM
AruM

Reputation: 252

Extract mail id from Script

Use this shell script as source to extract mail id fields . Need to extract the mail ids alone. For example need to take [email protected] [email protected] Please advice.

#!/bin/ksh
#exit 0
export nodename=`uname -n`
export [email protected]
mailx -s "ERROR: OWB loading for ${FILE_NAME} has failed " [email protected],[email protected] <<EOF

Upvotes: 0

Views: 478

Answers (4)

Chris
Chris

Reputation: 3047

Try:

Code:

awk -F\" '
    /^mailx/{
        n=split(substr($3,2,length($3)-6),emails,/,/)
        for (i=1;i<=n;i++)print emails[i]}
    ' f3

Output:

[email protected]
[email protected]

Upvotes: 0

dimir
dimir

Reputation: 793

If directory to scan (recursively) is MYDIR then:

grep -hrioI '[a-z0-9_\.]\+@[a-z0-9]\+\.[a-z0-9\.]\{2,\}' MYDIR 2>/dev/null | sort -u

If you only want to scan shell scripts add --include=*.sh option:

grep -hrioI --include=*.sh '[a-z0-9_\.]\+@[a-z0-9]\+\.[a-z0-9\.]\{2,\}' MYDIR 2>/dev/null | sort -u

EDIT I have changed the pattern for TLD to be at least 2 symbols ({2,} instead of +).

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77135

awk -v FS="[\",<]" '/mailx/{for(i=3;i<NF;i++) print $i}' file

If your files have similar formatting and you wish to extract all emails which follow the subject line until <

To capture sender_id then do something like this -

awk -v FS="[\",<$]" '
/mailx/{print "RECEPIENTS: ";for(i=4;i<NF;i++) print $i;next}
/SENDER_ID/{print "SENDER INFO: \n"$NF}' file

Test:

[jaypal:~/Temp] awk -v FS="[\",<$]" '
/mailx/{print "RECEPIENTS: ";for(i=4;i<NF;i++) print $i;next}
/SENDER_ID/{print "SENDER INFO: \n"$NF}' file
SENDER INFO: 
[email protected]
RECEPIENTS: 
 [email protected]
[email protected] 

Upvotes: 0

potong
potong

Reputation: 58473

This might work for you:

sed '/^mailx/!d;s/.*"\s*\|\s*\S*$//g;y/,/\n/' file1 file2 file3 ...
[email protected]
[email protected]

Upvotes: 0

Related Questions