AWK不打印到文件 [英] awk not printing to file

查看:184
本文介绍了AWK不打印到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

awk的新手在这里..
我想这样的:

awk newbie here.. I'm trying this:

top -b -p 30259 | awk 'BEGIN { OFS = ","; print "Timestamp,CPU,Memory"} /tomcat/ { print strftime("%H:%M:%S"), $9, $10 }' > asdf.log

但asdf.log始终保持为空。我试着重定向到从脚本内提交:

But 'asdf.log' always stays empty. I tried redirecting to file from within the script:

top -b -p 30259 | awk 'BEGIN { OFS = ","; print "Timestamp,CPU,Memory" > "asdf.log"} /tomcat/ { print strftime("%H:%M:%S"), $9, $10 > "asdf.log" }'

但它仍然没有工作...和打印到stdout是工作的罚款。

but it still doesn't work... And printing to stdout is working fine.

我在做什么错了?

推荐答案

多久,你等待数据的到来?

How long are you waiting for data to arrive?

在程序使用标准C库写入文件(的fopen(3)和家庭的功能),C库将添加一些的缓存应用于流来提高性能。如果每个读或一个字符的写真正启动系统调用,普通程序的性能将是pretty可怕。所以,C标准的输入程序将提交的的IO时的缓冲区完全够了。

When programs use the standard C library to write to files (fopen(3) and family of functions), the C library will add some buffering to the streams to improve performance. If every read or write of one character actually started a system call, the performance of common programs would be pretty horrible. So the C standard input routines will submit blocks of IO when the buffers are "full enough".

棘手的部分谈到了C标准IO例程将改变他们根据具体文件描述符不够充分的定义 - 如果文件描述符到的文件的,则是IO的块缓冲的,你必须等待一定量的数据到达的。 (检查与stat asdf.log的 IO模块条目看到的最有可能的大小。)但是,如果文件描述符是标准输出的它指的是一个终端设备,输出的行缓冲的 - 输出将被发送到终端只要一个换行符打印。

The tricky parts comes that the C standard IO routines will change their definition of "full enough" based on the specific file descriptors -- if the file descriptor is to a file, then the IO is block buffered, and you must wait for a certain amount of data to arrive. (Check stat asdf.log's IO Block entry to see the most likely size.) However, if the file descriptor is for stdout and it refers to a terminal device, the output is line buffered -- output will be sent to the terminal as soon as a newline character is printed.

也许 setvbuf用来(3)手册页可以解释比我更可以:

Perhaps the setvbuf(3) manpage can explain better than I can:

   The three types of buffering available are unbuffered, block
   buffered, and line buffered.  When an output stream is
   unbuffered, information appears on the destination file or
   terminal as soon as written; when it is block buffered many
   characters are saved up and written as a block; when it is
   line buffered characters are saved up until a newline is
   output or input is read from any stream attached to a
   terminal device (typically stdin).  The function fflush(3)
   may be used to force the block out early.  (See fclose(3).)
   Normally all files are block buffered.  When the first I/O
   operation occurs on a file, malloc(3) is called, and a buffer
   is obtained.  If a stream refers to a terminal (as stdout
   normally does) it is line buffered.  The standard error
   stream stderr is always unbuffered by default.

修改你的脚本是这样的:

Amend your script to look like this:

top -b | awk 'BEGIN { OFS = ","; print "Timestamp,CPU,Memory"}
/bash/ { print strftime("%H:%M:%S"), $9, $10; fflush() }' > /tmp/foo

fflush()力输出的立即的。不是很大的性能,但对于实时查看系统的状态更好。

The fflush() forces output immediately. Not great for performance, but better for viewing the state of your system in real time.

这篇关于AWK不打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆