My text file looks like below
date="2017-10-10" ip=192.168.1.1:22 inbound=100 outbound=100
date="2017-10-10" ip=192.168.1.1:22 inbound=100
date="2017-10-10" ip=192.168.1.1:22 outbound=100
I am using the below awk code to print the matched string and extract whatever is after the "=".
awk '{for(i=1;i<=NF;i++)if($i~/inbound=/)print $(i)}' | cut -d : -f1 | cut -d = -f2
For example I would search for "inbound=" and extract the "100". But the tricky part is "inbound" won't there in all the lines of the text Now I would like to print "0" if a line doesn't have the word "inbound".
Expected output
100
100
0 Not Found
Whenever you have name=value pairs in your input it's best to first create an array of those mappings (
f[]
below) and then you can just print (or do anything else with) the values by name:Want to do the same for "outbound" or any other field? Just init the name variable
n
accordingly"Input
Output
Explanation
Using GNU awk
In perl
In sed