How to filter and redirect output

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:



./myCMD 1>tmp


How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.







share|improve this question


























    up vote
    1
    down vote

    favorite












    I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:



    ./myCMD 1>tmp


    How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:



      ./myCMD 1>tmp


      How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.







      share|improve this question














      I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:



      ./myCMD 1>tmp


      How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 10:01









      user1404316

      2,314520




      2,314520










      asked Mar 7 at 9:06









      Yves

      705414




      705414




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          If you mean that you only want some lines to go to the tmp file, while the rest are left untouched:



          cmd | awk '/pattern/ print > "tmp"; next
          print'


          Or:



          cmd | sed '/pattern/!b
          w tmp
          d'


          (on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d)



          Or:



          cmd | sed '/pattern/ 
          w tmp
          d
          '


          (on one line: cmd | sed -e '/pattern/w tmp' -e 'd;')



          Note that for sed, pattern is a basic regular expression, while for awk, it's an extended regular expression.






          share|improve this answer



























            up vote
            2
            down vote













            You can do this with pipes:



            ./myCMD | grep keyword > tmp


            This will only write lines containing “keyword” to the tmp file.






            share|improve this answer




















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428695%2fhow-to-filter-and-redirect-output%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              If you mean that you only want some lines to go to the tmp file, while the rest are left untouched:



              cmd | awk '/pattern/ print > "tmp"; next
              print'


              Or:



              cmd | sed '/pattern/!b
              w tmp
              d'


              (on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d)



              Or:



              cmd | sed '/pattern/ 
              w tmp
              d
              '


              (on one line: cmd | sed -e '/pattern/w tmp' -e 'd;')



              Note that for sed, pattern is a basic regular expression, while for awk, it's an extended regular expression.






              share|improve this answer
























                up vote
                1
                down vote



                accepted










                If you mean that you only want some lines to go to the tmp file, while the rest are left untouched:



                cmd | awk '/pattern/ print > "tmp"; next
                print'


                Or:



                cmd | sed '/pattern/!b
                w tmp
                d'


                (on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d)



                Or:



                cmd | sed '/pattern/ 
                w tmp
                d
                '


                (on one line: cmd | sed -e '/pattern/w tmp' -e 'd;')



                Note that for sed, pattern is a basic regular expression, while for awk, it's an extended regular expression.






                share|improve this answer






















                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  If you mean that you only want some lines to go to the tmp file, while the rest are left untouched:



                  cmd | awk '/pattern/ print > "tmp"; next
                  print'


                  Or:



                  cmd | sed '/pattern/!b
                  w tmp
                  d'


                  (on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d)



                  Or:



                  cmd | sed '/pattern/ 
                  w tmp
                  d
                  '


                  (on one line: cmd | sed -e '/pattern/w tmp' -e 'd;')



                  Note that for sed, pattern is a basic regular expression, while for awk, it's an extended regular expression.






                  share|improve this answer












                  If you mean that you only want some lines to go to the tmp file, while the rest are left untouched:



                  cmd | awk '/pattern/ print > "tmp"; next
                  print'


                  Or:



                  cmd | sed '/pattern/!b
                  w tmp
                  d'


                  (on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d)



                  Or:



                  cmd | sed '/pattern/ 
                  w tmp
                  d
                  '


                  (on one line: cmd | sed -e '/pattern/w tmp' -e 'd;')



                  Note that for sed, pattern is a basic regular expression, while for awk, it's an extended regular expression.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 10:34









                  Stéphane Chazelas

                  280k53515847




                  280k53515847






















                      up vote
                      2
                      down vote













                      You can do this with pipes:



                      ./myCMD | grep keyword > tmp


                      This will only write lines containing “keyword” to the tmp file.






                      share|improve this answer
























                        up vote
                        2
                        down vote













                        You can do this with pipes:



                        ./myCMD | grep keyword > tmp


                        This will only write lines containing “keyword” to the tmp file.






                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          You can do this with pipes:



                          ./myCMD | grep keyword > tmp


                          This will only write lines containing “keyword” to the tmp file.






                          share|improve this answer












                          You can do this with pipes:



                          ./myCMD | grep keyword > tmp


                          This will only write lines containing “keyword” to the tmp file.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 7 at 9:08









                          Stephen Kitt

                          141k22307367




                          141k22307367






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428695%2fhow-to-filter-and-redirect-output%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Christian Cage

                              How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?