Artificial intelligent assistant

Bash: count occurrences of value in column x based on value in column y I have a string like this. data = "state:: 4 caller_contact:: sip:123456789@192.168.10.01:5080;transport=udp state:: 4 caller_contact:: sip:123456789@192.168.10.11:5080;transport=udp state:: 4 caller_contact:: sip:123456789@192.168.10.03:5080;transport=udp state:: 4 caller_contact:: sip:123456789@192.168.10.26:5080;transport=udp state:: 2 caller_contact:: sip:123456789@192.168.10.26:5080;transport=udp state:: 2 caller_contact:: sip:123456789@192.168.10.11:5080;transport=udp state:: 1 caller_contact:: sip:123456789@192.168.10.07:5080;transport=udp" I need to write a bash script to count how many times each IP e.g 192.168.26 have state 4 or state 2. (this string doesn't contain '/n') I am unable to parse this string and count values according to each IP.

I am not sure if this will work for every possible combination that you might have but it works for the small sample you provided:


sed "1,\$s/state/\
state/g" file | grep state > NewFile
for IPADDR in $(cat NewFile | cut -d"@" -f2|cut -d":" -f1|sort -n|uniq);do
for STATE in 2 4 ;do
LineCount=$(grep "${IPADDR}" NewFile |grep "state:: ${STATE}"| wc -l)
echo "For IP address ${IPADDR}, status:: ${STATE} lines count is ${LineCount}"
done
done | grep -v "is 0"$


you can add as many different STATE numbers you want on the for loop inside

Basically you are inserting a new line character before each occurrence of the string `state` making your big blob of data, separating in to multiple lines.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy b84ea4d2bacfb3e0b303926ae92ecc28