I have this text file with the following text content called output.txt:
1.2.2.2 LOCAL_IP
LOCAL_IP 1.1.1.1
1.1.1.1 LOCAL_IP
233.233.233.233 LOCAL_IP
123.123.123.123 LOCAL_IP
233.233.233.233 LOCAL_IP
231.231.231.231 LOCAL_IP
123.123.123.123 LOCAL_IP
LOCAL_IP 123.111.23.2
LOCAL_IP 221.22.22.22
1.1.1.1 LOCAL_IP
LOCAL_IP 1.2.2.2
LOCAL_IP 123.123.123.123
2.2.2.2 LOCAL_IP
LOCAL_IP 3.3.21.2
LOCAL_IP 2.2.2.2
1.2.2.2 LOCAL_IP
LOCAL_IP 123.123.123.123
LOCAL_IP 123.111.23.2
1.1.1.1 LOCAL_IP
I want to count the total occurrence of the IP above excluding the text string called LOCAL_IP. For example, this is my working code:
#!/bin/bash
output="output.txt"
a=$(awk '{ print $1 }' $output | grep -v 'LOCAL_IP' | sort | uniq -c | sed 's/^ *//' | sed -e "s/ /:/g")
b=$(awk '{ print $2 }' $output | grep -v 'LOCAL_IP' | sort | uniq -c | sed 's/^ *//' | sed -e "s/ /:/g")
echo "$a"
echo "-------"
echo "$b"
So this is the output printed from the above script (the first it prints a, and then b):
3:1.1.1.1
2:1.2.2.2
2:123.123.123.123
1:2.2.2.2
1:231.231.231.231
2:233.233.233.233
-------
1:1.1.1.1
1:1.2.2.2
2:123.111.23.2
2:123.123.123.123
1:221.22.22.22
1:2.2.2.2
1:3.3.21.2
Is there a way to combine the result of a variable a and b and then recalculate / update the total count for result a and b (before the symbol :) ? Then the result for the new variable c that has combined the a and b would be similar to this:
echo "$c"
4:1.1.1.1 # added from a and b
3:1.2.2.2
2:123.111.23.2
4:123.123.123.123
1:221.22.22.22
2:2.2.2.2
1:3.3.21.2
1:231.231.231.231
2:233.233.233.233
I'm not sure if I need to develop algorithm for this. Can someone shed some light maybe there is a simpler method to achieve this.