Format 'ps' command output without whitespace

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












8















I have the following ps command to get particular properties of all the running processes along with some properties:



ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args"


I wish to have it formatted in CSV so I can parse it. Note I have put the args at the end to make parsing easy; I don't think a , will exist in any of the other columns - please correct me if I am wrong.



How do I remove the whitespace?










share|improve this question




























    8















    I have the following ps command to get particular properties of all the running processes along with some properties:



    ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args"


    I wish to have it formatted in CSV so I can parse it. Note I have put the args at the end to make parsing easy; I don't think a , will exist in any of the other columns - please correct me if I am wrong.



    How do I remove the whitespace?










    share|improve this question


























      8












      8








      8


      1






      I have the following ps command to get particular properties of all the running processes along with some properties:



      ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args"


      I wish to have it formatted in CSV so I can parse it. Note I have put the args at the end to make parsing easy; I don't think a , will exist in any of the other columns - please correct me if I am wrong.



      How do I remove the whitespace?










      share|improve this question
















      I have the following ps command to get particular properties of all the running processes along with some properties:



      ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args"


      I wish to have it formatted in CSV so I can parse it. Note I have put the args at the end to make parsing easy; I don't think a , will exist in any of the other columns - please correct me if I am wrong.



      How do I remove the whitespace?







      csv ps






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 24 at 14:55









      Jeff Schaller

      43.8k1161141




      43.8k1161141










      asked Sep 1 '14 at 13:09









      CheetahCheetah

      173127




      173127




















          3 Answers
          3






          active

          oldest

          votes


















          7














          From the man page:



          -o format user-defined format.
          format is a single argument in the form of a blank-separated or comma-separated list, which offers a
          way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
          SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
          desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
          output. Column width will increase as needed for wide headers; this may be used to widen up columns
          such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
          (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
          output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
          when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
          DefBSD are macros that may be used to choose the default UNIX or BSD columns.


          So try:



          /bin/ps -o uname:1,ppid:1,pid:1





          share|improve this answer






























            5














            Since the first 6 fields should not contain blank characters (unless you allow them in user names), you can post-process the output:



            ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
            s/["]/\&/g
            s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,"/
            s/$/"/'


            Here quoting the last field (args) after having escaped the "s and s with .



            Produces an output like:



            stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo"bar"





            share|improve this answer
































              1














              You can use sed with ps. So what you want is here:-



              ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/ /,/g'


              But I wonder if it would be useful, as output of ps itself has got lots of ,.






              share|improve this answer























              • As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                – Cheetah
                Sep 1 '14 at 13:19











              • Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                – Oli
                Sep 1 '14 at 13:19











              • That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                – Stéphane Chazelas
                Sep 1 '14 at 13:30










              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',
              autoActivateHeartbeat: false,
              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%2f153157%2fformat-ps-command-output-without-whitespace%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









              7














              From the man page:



              -o format user-defined format.
              format is a single argument in the form of a blank-separated or comma-separated list, which offers a
              way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
              SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
              desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
              output. Column width will increase as needed for wide headers; this may be used to widen up columns
              such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
              (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
              output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
              when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
              DefBSD are macros that may be used to choose the default UNIX or BSD columns.


              So try:



              /bin/ps -o uname:1,ppid:1,pid:1





              share|improve this answer



























                7














                From the man page:



                -o format user-defined format.
                format is a single argument in the form of a blank-separated or comma-separated list, which offers a
                way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
                SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
                desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
                output. Column width will increase as needed for wide headers; this may be used to widen up columns
                such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
                (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
                output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
                when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
                DefBSD are macros that may be used to choose the default UNIX or BSD columns.


                So try:



                /bin/ps -o uname:1,ppid:1,pid:1





                share|improve this answer

























                  7












                  7








                  7







                  From the man page:



                  -o format user-defined format.
                  format is a single argument in the form of a blank-separated or comma-separated list, which offers a
                  way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
                  SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
                  desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
                  output. Column width will increase as needed for wide headers; this may be used to widen up columns
                  such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
                  (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
                  output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
                  when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
                  DefBSD are macros that may be used to choose the default UNIX or BSD columns.


                  So try:



                  /bin/ps -o uname:1,ppid:1,pid:1





                  share|improve this answer













                  From the man page:



                  -o format user-defined format.
                  format is a single argument in the form of a blank-separated or comma-separated list, which offers a
                  way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT
                  SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as
                  desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be
                  output. Column width will increase as needed for wide headers; this may be used to widen up columns
                  such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control
                  (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality;
                  output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options
                  when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and
                  DefBSD are macros that may be used to choose the default UNIX or BSD columns.


                  So try:



                  /bin/ps -o uname:1,ppid:1,pid:1






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 6 '15 at 1:26









                  Felipe AlvarezFelipe Alvarez

                  4341518




                  4341518























                      5














                      Since the first 6 fields should not contain blank characters (unless you allow them in user names), you can post-process the output:



                      ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
                      s/["]/\&/g
                      s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,"/
                      s/$/"/'


                      Here quoting the last field (args) after having escaped the "s and s with .



                      Produces an output like:



                      stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo"bar"





                      share|improve this answer





























                        5














                        Since the first 6 fields should not contain blank characters (unless you allow them in user names), you can post-process the output:



                        ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
                        s/["]/\&/g
                        s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,"/
                        s/$/"/'


                        Here quoting the last field (args) after having escaped the "s and s with .



                        Produces an output like:



                        stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo"bar"





                        share|improve this answer



























                          5












                          5








                          5







                          Since the first 6 fields should not contain blank characters (unless you allow them in user names), you can post-process the output:



                          ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
                          s/["]/\&/g
                          s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,"/
                          s/$/"/'


                          Here quoting the last field (args) after having escaped the "s and s with .



                          Produces an output like:



                          stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo"bar"





                          share|improve this answer















                          Since the first 6 fields should not contain blank characters (unless you allow them in user names), you can post-process the output:



                          ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed '
                          s/["]/\&/g
                          s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,/;s/ */,"/
                          s/$/"/'


                          Here quoting the last field (args) after having escaped the "s and s with .



                          Produces an output like:



                          stephane,3641,3702,10-00:20:24,0.1,0.3,"some cmd,and,args... VAR=foo"bar"






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Sep 1 '14 at 13:29

























                          answered Sep 1 '14 at 13:23









                          Stéphane ChazelasStéphane Chazelas

                          311k57586945




                          311k57586945





















                              1














                              You can use sed with ps. So what you want is here:-



                              ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/ /,/g'


                              But I wonder if it would be useful, as output of ps itself has got lots of ,.






                              share|improve this answer























                              • As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                                – Cheetah
                                Sep 1 '14 at 13:19











                              • Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                                – Oli
                                Sep 1 '14 at 13:19











                              • That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                                – Stéphane Chazelas
                                Sep 1 '14 at 13:30















                              1














                              You can use sed with ps. So what you want is here:-



                              ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/ /,/g'


                              But I wonder if it would be useful, as output of ps itself has got lots of ,.






                              share|improve this answer























                              • As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                                – Cheetah
                                Sep 1 '14 at 13:19











                              • Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                                – Oli
                                Sep 1 '14 at 13:19











                              • That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                                – Stéphane Chazelas
                                Sep 1 '14 at 13:30













                              1












                              1








                              1







                              You can use sed with ps. So what you want is here:-



                              ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/ /,/g'


                              But I wonder if it would be useful, as output of ps itself has got lots of ,.






                              share|improve this answer













                              You can use sed with ps. So what you want is here:-



                              ps --no-headers -exo "uname,ppid,pid,etime,%cpu,%mem,args" | sed 's/ /,/g'


                              But I wonder if it would be useful, as output of ps itself has got lots of ,.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 1 '14 at 13:17









                              beginerbeginer

                              2,0681117




                              2,0681117












                              • As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                                – Cheetah
                                Sep 1 '14 at 13:19











                              • Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                                – Oli
                                Sep 1 '14 at 13:19











                              • That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                                – Stéphane Chazelas
                                Sep 1 '14 at 13:30

















                              • As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                                – Cheetah
                                Sep 1 '14 at 13:19











                              • Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                                – Oli
                                Sep 1 '14 at 13:19











                              • That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                                – Stéphane Chazelas
                                Sep 1 '14 at 13:30
















                              As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                              – Cheetah
                              Sep 1 '14 at 13:19





                              As I mentioned, I believe I have only chosen one column that could have a , which I have put at the end, however, this solution would remove the , from that column which I do not want.

                              – Cheetah
                              Sep 1 '14 at 13:19













                              Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                              – Oli
                              Sep 1 '14 at 13:19





                              Look at the output that generates and tell me if you think that's desirable :) Especially take notice of the command column.

                              – Oli
                              Sep 1 '14 at 13:19













                              That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                              – Stéphane Chazelas
                              Sep 1 '14 at 13:30





                              That would be the same as tr ' ' ,. You probably want tr -s ' ' , here.

                              – Stéphane Chazelas
                              Sep 1 '14 at 13:30

















                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f153157%2fformat-ps-command-output-without-whitespace%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?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?