How can I parse the output of `ps` to different fields?

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











up vote
0
down vote

favorite












I would like to parse the output of ps, to get any field using awk. It seems that ps separate the fields by whitespaces, but the values in COMMAND field usually have whitespaces.



Is there a way to tell ps to use some distinctive field separator in its output? I haven't found one in ps manpage.



If not, what can I do to get the values in a field? Thanks.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I would like to parse the output of ps, to get any field using awk. It seems that ps separate the fields by whitespaces, but the values in COMMAND field usually have whitespaces.



    Is there a way to tell ps to use some distinctive field separator in its output? I haven't found one in ps manpage.



    If not, what can I do to get the values in a field? Thanks.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to parse the output of ps, to get any field using awk. It seems that ps separate the fields by whitespaces, but the values in COMMAND field usually have whitespaces.



      Is there a way to tell ps to use some distinctive field separator in its output? I haven't found one in ps manpage.



      If not, what can I do to get the values in a field? Thanks.










      share|improve this question















      I would like to parse the output of ps, to get any field using awk. It seems that ps separate the fields by whitespaces, but the values in COMMAND field usually have whitespaces.



      Is there a way to tell ps to use some distinctive field separator in its output? I haven't found one in ps manpage.



      If not, what can I do to get the values in a field? Thanks.







      text-processing awk ps






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 4 at 21:27

























      asked Dec 4 at 21:11









      Tim

      25.4k73243447




      25.4k73243447




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can first replace multiple spaces with a single space (or any other character). See this question on how to accomplish this. Next you can use cut to select the column(s) you want. See also this question.






          share|improve this answer




















          • Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
            – Tim
            Dec 4 at 21:15











          • Not sure about that one.
            – Tom
            Dec 4 at 21:16

















          up vote
          1
          down vote













          You can explicitly specify your own field separator in the format string argument when utilizing a printf() style user-defined output:



          $ ps -o '%p,%a,%c,%y'
          PID,COMMAND ,COMMAND ,TTY
          15742,ps -o %p,%a,%c,%y ,ps ,pts/6
          24719,-su ,bash ,pts/6





          share|improve this answer



























            up vote
            1
            down vote













            You could make use of the fact that a shell read places ALL residual fields into the last variable, and print COMMAND last.



            ps -eo pid,cmd,tty,args | while read PID CMD TTY ARG; do echo $PID $CMD $TTY $ARG; done


            You can convey $ARG to awk, or else concatenate $4 ... $NF within awk when reading the ps output directly






            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: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f485999%2fhow-can-i-parse-the-output-of-ps-to-different-fields%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              You can first replace multiple spaces with a single space (or any other character). See this question on how to accomplish this. Next you can use cut to select the column(s) you want. See also this question.






              share|improve this answer




















              • Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
                – Tim
                Dec 4 at 21:15











              • Not sure about that one.
                – Tom
                Dec 4 at 21:16














              up vote
              1
              down vote













              You can first replace multiple spaces with a single space (or any other character). See this question on how to accomplish this. Next you can use cut to select the column(s) you want. See also this question.






              share|improve this answer




















              • Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
                – Tim
                Dec 4 at 21:15











              • Not sure about that one.
                – Tom
                Dec 4 at 21:16












              up vote
              1
              down vote










              up vote
              1
              down vote









              You can first replace multiple spaces with a single space (or any other character). See this question on how to accomplish this. Next you can use cut to select the column(s) you want. See also this question.






              share|improve this answer












              You can first replace multiple spaces with a single space (or any other character). See this question on how to accomplish this. Next you can use cut to select the column(s) you want. See also this question.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 4 at 21:14









              Tom

              1217




              1217











              • Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
                – Tim
                Dec 4 at 21:15











              • Not sure about that one.
                – Tom
                Dec 4 at 21:16
















              • Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
                – Tim
                Dec 4 at 21:15











              • Not sure about that one.
                – Tom
                Dec 4 at 21:16















              Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
              – Tim
              Dec 4 at 21:15





              Thanks. Does ps always separate two fields by more than one whitespaces? Does a value under COMMAND never contain more than one consecutive whitespaces?
              – Tim
              Dec 4 at 21:15













              Not sure about that one.
              – Tom
              Dec 4 at 21:16




              Not sure about that one.
              – Tom
              Dec 4 at 21:16












              up vote
              1
              down vote













              You can explicitly specify your own field separator in the format string argument when utilizing a printf() style user-defined output:



              $ ps -o '%p,%a,%c,%y'
              PID,COMMAND ,COMMAND ,TTY
              15742,ps -o %p,%a,%c,%y ,ps ,pts/6
              24719,-su ,bash ,pts/6





              share|improve this answer
























                up vote
                1
                down vote













                You can explicitly specify your own field separator in the format string argument when utilizing a printf() style user-defined output:



                $ ps -o '%p,%a,%c,%y'
                PID,COMMAND ,COMMAND ,TTY
                15742,ps -o %p,%a,%c,%y ,ps ,pts/6
                24719,-su ,bash ,pts/6





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can explicitly specify your own field separator in the format string argument when utilizing a printf() style user-defined output:



                  $ ps -o '%p,%a,%c,%y'
                  PID,COMMAND ,COMMAND ,TTY
                  15742,ps -o %p,%a,%c,%y ,ps ,pts/6
                  24719,-su ,bash ,pts/6





                  share|improve this answer












                  You can explicitly specify your own field separator in the format string argument when utilizing a printf() style user-defined output:



                  $ ps -o '%p,%a,%c,%y'
                  PID,COMMAND ,COMMAND ,TTY
                  15742,ps -o %p,%a,%c,%y ,ps ,pts/6
                  24719,-su ,bash ,pts/6






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 4 at 21:33









                  Derek Callaway

                  743




                  743




















                      up vote
                      1
                      down vote













                      You could make use of the fact that a shell read places ALL residual fields into the last variable, and print COMMAND last.



                      ps -eo pid,cmd,tty,args | while read PID CMD TTY ARG; do echo $PID $CMD $TTY $ARG; done


                      You can convey $ARG to awk, or else concatenate $4 ... $NF within awk when reading the ps output directly






                      share|improve this answer
























                        up vote
                        1
                        down vote













                        You could make use of the fact that a shell read places ALL residual fields into the last variable, and print COMMAND last.



                        ps -eo pid,cmd,tty,args | while read PID CMD TTY ARG; do echo $PID $CMD $TTY $ARG; done


                        You can convey $ARG to awk, or else concatenate $4 ... $NF within awk when reading the ps output directly






                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You could make use of the fact that a shell read places ALL residual fields into the last variable, and print COMMAND last.



                          ps -eo pid,cmd,tty,args | while read PID CMD TTY ARG; do echo $PID $CMD $TTY $ARG; done


                          You can convey $ARG to awk, or else concatenate $4 ... $NF within awk when reading the ps output directly






                          share|improve this answer












                          You could make use of the fact that a shell read places ALL residual fields into the last variable, and print COMMAND last.



                          ps -eo pid,cmd,tty,args | while read PID CMD TTY ARG; do echo $PID $CMD $TTY $ARG; done


                          You can convey $ARG to awk, or else concatenate $4 ... $NF within awk when reading the ps output directly







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 4 at 22:06









                          RudiC

                          3,8891312




                          3,8891312



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f485999%2fhow-can-i-parse-the-output-of-ps-to-different-fields%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

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

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay