How to pipe awk output (with periodic, continuous input) to output file?
I am trying to write a command that pipes the continuous output of a free command (run every second) to an awk command that parses a specific value (available free memory) and outputs this to a file with a timestamp. Here are my current attempts at the command:
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 }' >>memOut
And alternatively, after a bit of Googling
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 >>"memOut"}'
Each run produces empty files. Any suggestions or possibly different methods?
awk pipe
add a comment |
I am trying to write a command that pipes the continuous output of a free command (run every second) to an awk command that parses a specific value (available free memory) and outputs this to a file with a timestamp. Here are my current attempts at the command:
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 }' >>memOut
And alternatively, after a bit of Googling
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 >>"memOut"}'
Each run produces empty files. Any suggestions or possibly different methods?
awk pipe
add a comment |
I am trying to write a command that pipes the continuous output of a free command (run every second) to an awk command that parses a specific value (available free memory) and outputs this to a file with a timestamp. Here are my current attempts at the command:
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 }' >>memOut
And alternatively, after a bit of Googling
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 >>"memOut"}'
Each run produces empty files. Any suggestions or possibly different methods?
awk pipe
I am trying to write a command that pipes the continuous output of a free command (run every second) to an awk command that parses a specific value (available free memory) and outputs this to a file with a timestamp. Here are my current attempts at the command:
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 }' >>memOut
And alternatively, after a bit of Googling
free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 >>"memOut"}'
Each run produces empty files. Any suggestions or possibly different methods?
awk pipe
awk pipe
edited Jul 14 '14 at 17:04
Cfinley
1,43331120
1,43331120
asked Jan 16 '12 at 19:19
Mark
255127
255127
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
add a comment |
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".
add a comment |
Just to make sure that you are getting what you actually want and not what you specifically asked.
If you want to know available memory on the system for programs then this might be more suitable:
free -m -s 1 | awk '/buffers/cache/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut
Mem line's Used column includes caches and buffers and in most cases when you want to monitor memory usage for given computer/task those should at least be noted.
add a comment |
On certain versions of awk
(e.g. mawk 1.3.3) you need to add the -W interactive
command line flag to enable unbuffered operation with pipes.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f379122%2fhow-to-pipe-awk-output-with-periodic-continuous-input-to-output-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
add a comment |
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
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
add a comment |
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
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
edited Jan 16 '12 at 20:00
answered Jan 16 '12 at 19:48
cYrus
15.7k55168
15.7k55168
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
add a comment |
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
This gave me the output file I wanted. Thank you!
– Mark
Jan 16 '12 at 19:54
add a comment |
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".
add a comment |
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".
add a comment |
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".
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".
answered Feb 4 '13 at 10:29
Steve Schnepp
3211510
3211510
add a comment |
add a comment |
Just to make sure that you are getting what you actually want and not what you specifically asked.
If you want to know available memory on the system for programs then this might be more suitable:
free -m -s 1 | awk '/buffers/cache/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut
Mem line's Used column includes caches and buffers and in most cases when you want to monitor memory usage for given computer/task those should at least be noted.
add a comment |
Just to make sure that you are getting what you actually want and not what you specifically asked.
If you want to know available memory on the system for programs then this might be more suitable:
free -m -s 1 | awk '/buffers/cache/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut
Mem line's Used column includes caches and buffers and in most cases when you want to monitor memory usage for given computer/task those should at least be noted.
add a comment |
Just to make sure that you are getting what you actually want and not what you specifically asked.
If you want to know available memory on the system for programs then this might be more suitable:
free -m -s 1 | awk '/buffers/cache/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut
Mem line's Used column includes caches and buffers and in most cases when you want to monitor memory usage for given computer/task those should at least be noted.
Just to make sure that you are getting what you actually want and not what you specifically asked.
If you want to know available memory on the system for programs then this might be more suitable:
free -m -s 1 | awk '/buffers/cache/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut
Mem line's Used column includes caches and buffers and in most cases when you want to monitor memory usage for given computer/task those should at least be noted.
answered Jan 16 '12 at 22:01
Manwe
868412
868412
add a comment |
add a comment |
On certain versions of awk
(e.g. mawk 1.3.3) you need to add the -W interactive
command line flag to enable unbuffered operation with pipes.
add a comment |
On certain versions of awk
(e.g. mawk 1.3.3) you need to add the -W interactive
command line flag to enable unbuffered operation with pipes.
add a comment |
On certain versions of awk
(e.g. mawk 1.3.3) you need to add the -W interactive
command line flag to enable unbuffered operation with pipes.
On certain versions of awk
(e.g. mawk 1.3.3) you need to add the -W interactive
command line flag to enable unbuffered operation with pipes.
answered Dec 15 at 15:50
Pierz
58058
58058
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f379122%2fhow-to-pipe-awk-output-with-periodic-continuous-input-to-output-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown