0

I created a nice little command to measure data bandwidth consumption over a metered connection (excluding local traffic):

sudo iftop -i enp1s0 -f 'not (src net (10 or 172.16/12 or 192.168/16) and dst net (10 or 172.16/12 or 192.168/16))' -t -L1 2> /dev/null | awk '$1 == "Cumulative" {print $5 ;}'

If you give it some time you will see it will start printing lines with data in bytes (stdout I guess)..

I have tried many things trying to write these lines to a file including several variations of 1>, >, >>, tee and more.. Nothing seems to work, on redirecting output from awk.

2 Answers2

1

Your problem is that if awk detects that its output is not a terminal, it switches to buffered output; you just have to wait longer for any output to appear.

If you don't want to wait, use:

sudo iftop -i enp1s0 -f 'not (src net (10 or 172.16/12 or 192.168/16) and dst net (10 or 172.16/12 or 192.168/16))' -t -L1 2> /dev/null | awk '$1 == "Cumulative" {print $5; fflush(); }'

(The fflush() will cause all lines to be printed immediately, which is less efficient but allows you to see it immediately.)

András Korn
  • 641
  • 5
  • 13
0

Mixing redirections with sudo can be confusing. I'd run the pipeline in a new shell, and the redirection can go there:

sudo sh <<'END_IFTOP'
    filter="not (src net (10 or 172.16/12 or 192.168/16) and dst net (10 or 172.16/12 or 192.168/16))"
    iftop -i enp1s0 -f "$filter" -t -L1 2> /dev/null |
    awk '$1 == "Cumulative" {print $5}' > $HOME/output.file
END_IFTOP
glenn jackman
  • 4,320
  • 16
  • 19