Skip to main content

Getting only last line of continuous stdout [Resolved]

I'm using sar to collect network statistics from my wlp2s0 interface (1 second interval), like so:

$ sar -n DEV 1 | grep wlp2s0

and the continuous output looks like this:

18:15:06       wlp2s0      0,00      0,00      0,00      0,00      0,00      0,00      0,00      0,00
18:15:07       wlp2s0      1,00      2,00      0,12      0,28      0,00      0,00      0,00      0,00
18:15:08       wlp2s0      0,00      0,00      0,00      0,00      0,00      0,00      0,00      0,00
18:15:09       wlp2s0      0,00      0,00      0,00      0,00      0,00      0,00      0,00      0,00
18:15:10       wlp2s0      0,00      1,00      0,00      0,24      0,00      0,00      0,00      0,00
18:15:11       wlp2s0      0,00      1,00      0,00      0,24      0,00      0,00      0,00      0,00

I want to write only the last line of the output (the current measurement) in a .txt file. The thing is, I don't know how to construct a command to do this.

The only way I can write to a file (which is continuously increasing its size, and that's not what I want) is by doing:

$ sar -n DEV 1 > output.txt

The stdout is always increasing and because I'm overwriting the file, all previous and upcoming lines will be written.

[EDIT] My problem is not dependent on this tool (sar), it happened with any tool with continuous output. I've heard of circular buffers, but I don't think it would work in this case.


Question Credit: imll
Question Reference
Asked July 21, 2019
Tags: stdout
Posted Under: Unix Linux
7 views
2 Answers

sar -n DEV 1 |
  perl -ne 'if(/wlp/) { open(O,">current"); print O; close O }'

credit: Ole Tange
Answered July 21, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA