I'm using this command:

sar 1 | tail -n +3

which outputs the following (every 1 second, I interrupted it after few seconds):

root@debian:/home/hyper/stats# sar 1 | tail -n +3
20:00:04        CPU     %user     %nice   %system   %iowait    %steal     %idle
20:00:05        all      0,25      0,00      0,50      0,00      0,00     99,25
20:00:06        all      0,50      0,00      0,25      0,00      0,00     99,25
20:00:07        all      4,79      0,00      1,01      0,00      0,00     94,21
20:00:08        all      0,75      0,00      0,75      0,75      0,00     97,74
20:00:09        all      1,26      0,00      0,76      0,00      0,00     97,98
20:00:10        all      0,75      0,00      0,50      0,00      0,00     98,74
^C

The problem is when I try to write this output to file. I tried with:

sar 1 | tail -n +3 > file

But it creates an empty file. The problem is the "sar" command that generates a "continuous output", but I don't know how to handle it.

EDIT: to clarify what I want to do: "I run a command, like "top" (or "sar 1" etc.), which produces an output every X seconds. Every produced output contains an header (few lines) that I don't want. So I want to run "top" for 10 seconds and save the 10 produced outputs without their headers to a file."

有帮助吗?

解决方案

Instead of using tail, you can use a shell command list to strip out the first few lines:

sar 1 | { read; read; cat; } > file.txt

I'm not sure why tail is not working; I though perhaps you were just missing the -f option, but that doesn't seem to produce any output either.

其他提示

sar 1 >> filename

or

sar 1 | tee -a filename


You can use the tee -a command to output to your console (STDOUT) and to append that output to a file at the same time.

You can append to the file without overriding it by using >>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top