petzi.at
petzi.at

Reputation: 1

How I proceed with awk after if statement

my input:

Jun 26 06:54:33 host dovecot: imap-login: Login: user=<xxx>, method=PLAIN, rip=111.111.111.111, lip=111.111.111.111, mpid=00000, TLS, session=<LVVIgfWodFBZD+3W>

Like to get the IP of the rip entry with one command

awk '{ if ($6 == "imap-login:" && match($10,/rip/) ) { print $10 }  }'

give me "rip=78.47.14.44,"

How it works to get only the IP?

Upvotes: 0

Views: 65

Answers (2)

Ed Morton
Ed Morton

Reputation: 204731

$ awk -F'[ =,]+' '$6=="imap-login:"{print $13}' file
111.111.111.111

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133780

Could you please try following, written and tested with shown samples in GNU awk.

awk 'match($0,/rip[^,]*/){print substr($0,RSTART+4,RLENGTH-4)}' Input_file

OR as per kvantour sir's suggestion:

awk 'match($0,/[:,] rip[^,]*/){val=substr($0,RSTART+4,RLENGTH-4);sub(/.*rip/,"");print val;val=""}' Input_file

Explanation: Adding detailed explanation for above.

awk '                                      ##Starting awk program from here.
match($0,/rip[^,]*/){                      ##Using match to match regex of rip till comma comes in current line,if regex match is found then it sets variables RSTART and RLENGTH for match.
  print substr($0,RSTART+4,RLENGTH-4)      ##Printing sub string from RSTART+4 value to RLENGTH+4 values to get exact IP which is coming with strig rip in line.
}
' Input_file                               ##Mentioning Input_file name here.

Upvotes: 2

Related Questions