简体   繁体   English

如何写“命令-i interval | tail“输出到文件

[英]How to write “command -i interval | tail” output to file

I'm using this command: 我正在使用此命令:

sar 1 | tail -n +3

which outputs the following (every 1 second, I interrupted it after few seconds): 输出以下内容(每1秒钟,我在几秒后中断它):

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. 问题是生成“连续输出”的“sar”命令,但我不知道如何处理它。

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." 编辑:澄清我想做的事情:“我运行一个命令,比如”top“(或”sar 1“等),它每X秒产生一个输出。每个产生的输出都包含一个标题(几行)我想要运行“top”10秒钟,并将10个生成的输出保存到文件中。“

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. 您可以使用tee -a命令输出到控制台( STDOUT )并同时将该输出附加到文件。

You can append to the file without overriding it by using >> 您可以使用>>来附加到文件而不覆盖它

Instead of using tail , you can use a shell command list to strip out the first few lines: 您可以使用shell命令列表删除前几行,而不是使用tail

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

I'm not sure why tail is not working; 我不确定tail为什么不起作用; I though perhaps you were just missing the -f option, but that doesn't seem to produce any output either. 我可能你只是错过了-f选项,但这似乎也没有产生任何输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM