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.

1
  • Give output of your command. Commented Jun 3, 2019 at 15:06

2 Answers 2

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.)

1
  • The fflush() did it for me :) Commented Mar 6, 2022 at 7:35
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 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.