Ashish Khurana
Ashish Khurana

Reputation: 3

search variable and corresponding value in bash

I have below sample file:

Jul 16 00:01:24  abc postfix/smtp[28719]: 51AEqwqwq06: to=<[email protected]>, relay=none, delay=0.17, delays=0.17/0/0/0, dsn=4.4.1, status=deferred (connect to 127.0.0.1[127.0.0.1]:10026: Connection refused)
Jul 16 00:01:36  abc postfix/smtp[28655]: E444qw002: to=<[email protected]>, relay=in.emailct.com[63.20.111.76]:25, delay=39, delays=0.06/0/0.92/38, dsn=2.1.5, status=deliverable (250 2.1.5 [email protected] )
Jul 16 00:01:43  abc postfix/smtp[28815]: F19Dwq003: to=<[email protected]>, relay=em1.SullivanCromwell.com[223.222.222.2]:25, delay=162708, delays=162705/0.3/1.6/0.62, dsn=4.2.2, status=deferred (host em1.SullivanCromwell.com[223.222.222.2] said: 452 4.2.2 Mailbox full (in reply to RCPT TO command))

I want to display highest integer value of "delay=" and corresponding line from file.

sample output:

longest delay was: **162708** on and at **Jul 16 00:01:43** on server **abc**0

Jul 16 00:01:43  postfix/smtp[28815]: F19Dwq003: to=<[email protected]>, relay=emm.SullivanCromwell.com[205.22.33.33]:25, delay=162708, delays=162705/0.3/1.6/0.62, dsn=4.2.2, status=deferred (host emm.SullivanCromwell.com[223.222.222.2] said: 452 4.2.2 Mailbox full (in reply to RCPT TO command))

Thanks in advance

Upvotes: 0

Views: 84

Answers (1)

fedorqui
fedorqui

Reputation: 289625

All together:

$ data=$(grep -nPo '(?<=delay=)\d+' file | sort -rn -t: -k2 | head -1)
$ line=${data%%:*}
$ delay=${data##*:}
$ awk -v line=$line -v delay=$delay 'NR==line {print "longest delay was **", delay, "** on", $1, $2, "at", $3, "at server",$4, "\n\n",$0}' file
longest delay was ** 162708 ** on Jul 16 at 00:01:43 at server abc 

 Jul 16 00:01:43  abc postfix/smtp[28815]: F19Dwq003: to=<[email protected]>, relay=em1.SullivanCromwell.com[223.222.222.2]:25, delay=162708, delays=162705/0.3/1.6/0.62, dsn=4.2.2, status=deferred (host em1.SullivanCromwell.com[223.222.222.2] said: 452 4.2.2 Mailbox full (in reply to RCPT TO command))

Explanation (spoiler: it can be boring)

You can firstly get the following:

$ grep -nPo '(?<=delay=)\d+' file | sort -rn -t: -k2
3:162708
2:39
1:0

Let's split it in parts:

$ grep -nPo '(?<=delay=)\d+' file
1:0
2:39
3:162708

gives the number of line and the value of delay=. As we just want the first line, we do head -1. Then we sort it by number with sort -rn -t: -k2. First row will be line number:delay.

So we have the following to get the delay and line:

$ grep -nPo '(?<=delay=)\d+' file | sort -rn -t: -k2 | head -1
3:162708

And

$ line=${data%%:*}  # returns value after :
$ delay=${data##*:} # returns value before :

Then it is time for awk:

awk -v line=$line -v delay=$delay 'NR==line {print "longest delay was **", delay, "** on", $1, $2, "at", $3, "at server",$4}' file
  • awk -v var=$some_var gives to awk the value of a session var to be used inside the command.
  • NR==line {} makes the actions to be performed just when it is in the line line of the file.
  • {print "longest delay was **", delay, "** on", $1, $2, "at", $3, "at server",$4, "\n\n",$0} prints the data the way it is asked in the question. $1, $2... correspond to the field position in the line. $0 corresponds to the full line.

Upvotes: 1

Related Questions