Pergunta

Here is the output of my netstat command. I want to count total of first field number like 7+8+1+1+1+1+3+1+2..so on... How do i use bc or any other method command to total count them?

[root@example httpd]# netstat -natp | grep 7143 | grep EST | awk -F' ' '{print $5}' | awk -F: '{print $1}' | sort -nr | uniq -c
      7 209.139.35.xxx
      8 209.139.35.xxx
      1 209.139.35.xxx
      1 209.139.35.xxx
      1 208.46.149.xxx
      3 96.17.177.xxx
      1 96.17.177.xxx
      2 96.17.177.xxx
Foi útil?

Solução

You need to get the first column with awk (You don't actually need this, but I'm leaving it as a monument to my eternal shame)

awk {'print $1'} 

and then use awk again to sum the column of numbers and print the result

awk '{ sum+=$1} END {print sum}'

All together:

netstat -natp | grep 7143 | grep EST | awk -F' ' '{print $5}' | awk -F: '{print $1}' | sort -nr | uniq -c | awk {'print $1'}  | awk '{ sum+=$1} END {print sum}'

I know this doesn't use bc, but it gets the job done, so hopefully that's enough.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top