How to pipe awk output (with periodic, continuous input) to output file?












9














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?










share|improve this question





























    9














    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?










    share|improve this question



























      9












      9








      9


      4





      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 14 '14 at 17:04









      Cfinley

      1,43331120




      1,43331120










      asked Jan 16 '12 at 19:19









      Mark

      255127




      255127






















          4 Answers
          4






          active

          oldest

          votes


















          12














          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





          share|improve this answer























          • This gave me the output file I wanted. Thank you!
            – Mark
            Jan 16 '12 at 19:54



















          3














          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".






          share|improve this answer





























            2














            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.






            share|improve this answer





























              0














              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.






              share|improve this answer





















                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
                });


                }
                });














                draft saved

                draft discarded


















                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









                12














                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





                share|improve this answer























                • This gave me the output file I wanted. Thank you!
                  – Mark
                  Jan 16 '12 at 19:54
















                12














                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





                share|improve this answer























                • This gave me the output file I wanted. Thank you!
                  – Mark
                  Jan 16 '12 at 19:54














                12












                12








                12






                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





                share|improve this answer














                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                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


















                • 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













                3














                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".






                share|improve this answer


























                  3














                  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".






                  share|improve this answer
























                    3












                    3








                    3






                    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".






                    share|improve this answer












                    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".







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 4 '13 at 10:29









                    Steve Schnepp

                    3211510




                    3211510























                        2














                        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.






                        share|improve this answer


























                          2














                          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.






                          share|improve this answer
























                            2












                            2








                            2






                            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.






                            share|improve this answer












                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 16 '12 at 22:01









                            Manwe

                            868412




                            868412























                                0














                                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.






                                share|improve this answer


























                                  0














                                  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.






                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    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.






                                    share|improve this answer












                                    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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 15 at 15:50









                                    Pierz

                                    58058




                                    58058






























                                        draft saved

                                        draft discarded




















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                        Aardman Animations

                                        Are they similar matrix