Reputation: 33
I have an issue using awk and sed to parse a log file.
The below file:
DDDD;callID:4565;AAAA;Body:test message 1;BBBB;ng?10.1.5.60 4565;complete
DDDD;callID:5489;AAAA;Body:test message 2;BBBB;ng?10.4.100.3 5489;complete
DDDD;callID:3456;AAAA;Body:test message 3;BBBB;ng?10.200.1.5 3456;failed
Using the following command works fine to strip out IP from column 7. I am relying on removing last 5 characters from end to output only IP.
`awk -F";" ' {print $2 ";" $4 ";" $7 ";" $6}' test.CSV | sed -e 's/Body://g; s/callID://g; s/ng?//g' | sed 's/.\{5\}$//' >> complete.txt`
output as expeted:
4565;test message 1;complete;10.1.5.60
5489;test message 2;complete;10.4.100.3
3456;test message 3;failed;10.200.1.5
9 out of 10 times this is fine, but just found out column 7 will not always contain 4 digits at the end. Column 7 contains user input data so if something larger than 4 digit is entered incorrectly, it does not strip the IP as needed. How can I remove everything but the IP for that column?
Example of incorrect field:
DDDD;callID:3456;AAAA;Body:test message 3;BBBB;ng?10.200.1.5 3456345;failed
bad output:
3456;test message 3;failed;10.200.1.5 34
I was trying to use
grep -Eo '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b'
inside the {print.....} section but could not get it to work.....
thank you in advance for your help.
Upvotes: 0
Views: 842
Reputation: 204628
$ awk -F'[:;?]' -v OFS=';' '{sub(/ .*/,"",$9); print $3,$6,$10,$9}' file
4565;test message 1;complete;10.1.5.60
5489;test message 2;complete;10.4.100.3
3456;test message 3;failed;10.200.1.5
Upvotes: 1
Reputation: 45343
Using awk
awk '{split($2,a,":");split($4,b,":");split($6,c,"[? ]");print a[2],b[2],$7,c[2]}' FS=";" OFS=";" file
4565;test message 1;complete;10.1.5.60
5489;test message 2;complete;10.4.100.3
3456;test message 3;failed;10.200.1.5
Upvotes: 0