How to use invert match in awk along with other patterns

kulfi :

How to use invert match with awk like grep -v , i have below pattern mix of awk, sed, grep and sort.

Is there a pure awk or sed way to do the below..

$ awk '/status=sent/{print $1,$2,$7,$8|"sort -u"}' /var/log/maillog-20150308 |sed 's/to=<//g' | sed 's/>,//g' | grep -v "relay=local"

Sample log..

Mar 15 09:00:12 testhost postfix/local[11995]: 7245441DF1: to=<[email protected]>, relay=local, delay=0.02, delays=0/0/0/0.02, dsn=2.0.0, status=sent (delivered to file: /dev/null)
Mar 15 09:00:12 testhost postfix/local[11995]: 7245441DF1: to=<[email protected]>, relay=mysmtp.com, delay=0.02, delays=0/0/0/0.02, dsn=2.0.0, status=sent (delivered to file: /dev/null)

Current and desired Output:

$ awk '/status=sent/{print $1,$2,$7,$8|"sort -u"}' /tmp/test |sed 's/to=<//g' | sed 's/>,//g' | grep -v "relay=local"
Mar 15 [email protected] relay=mysmtp.com,
Mar 7 [email protected] [email protected]
James Brown :

This is one way of doing it:

$ awk '
$12=="status=sent" && $8!="relay=local," {  # process only matching records
    gsub(/^to=<|>,$/,"",$7)                 # tune that $7
    b=$1 OFS $2 OFS $7 OFS $8               # buffer output record for
    if(!a[b]++)                             # ... uniq record 
        print b                             # ... output
}' file

Output with given sample data:

Mar 15 [email protected] relay=mysmtp.com,

Updated with a one-liner version:

$ awk '$12=="status=sent"&&$8!="relay=local,"{gsub(/^to=<|>,$/,"",$7);b=$1 OFS $2 OFS $7 OFS $8;if(!a[b]++)print b}' file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350453&siteId=1