How to pipe awk output (with periodic, continuous input) to output file?
Solution 1:
You have to flush the buffer to see something in memOut
during the execution:
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut
Here's an alternative version:
while sleep 1; do sed -n "s/MemFree: */`date`, /p" /proc/meminfo; done >> memOut
Solution 2:
For old versions of awk
you may have to use system("")
.
Actually, fflush(stdout)
is for only for recent versions of awk
and gawk
, as its only in the POSIX standard since December 2012.
free -mto -s 1 | awk '/Mem/ {
print strftime("%r") "," $4;
system(""); # Flush output
}' >> memOut
Note that using system("")
flushes every file descriptor, its full description is in the gawk
manual, section "9.1.4 Input/Output Functions".