Find the answer to your Linux question:
Results 1 to 8 of 8
Hi guys, I have a problem when parsing the ps output data. Because there's no unique delimit between two fields(some field use spaces too, args for example), so I look ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    3

    How to modify ps output format?

    Hi guys,
    I have a problem when parsing the ps output data. Because there's no unique delimit between two fields(some field use spaces too, args for example), so I look through the man page to find how it can take a user-define delimit between two fields, but I find nothing.
    Someone has any ideas? thank you very much!

  2. #2
    Just Joined! srerucha's Avatar
    Join Date
    Jun 2005
    Location
    Brno, Czech republic
    Posts
    58
    Hi,
    you haven't mentioned what do you use for parsing ...

    If you use bash script, look for shell variable IFS (Internal Field Separator).

    S.

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    3
    I'm parsing it in C program, and I get the ps output through a pipe
    So I can't set IFS, however,thank you very much

  4. #4
    Just Joined! srerucha's Avatar
    Join Date
    Jun 2005
    Location
    Brno, Czech republic
    Posts
    58
    Well, you can read the output line by line. On each line you know the number of fields and you can IMHO suppose, that spaces occur only in the last field (program and arguments). So you can read through the line, until you reach first character of the last field. From that character everything up to a newline character is part of that field.

    Anyway, have a look on the output of the command:

    Code:
    strace ps
    Youĺl probably get list of such triplets

    Code:
    open("/proc/4209/status", O_RDONLY)     = 6
    read(6, "Name:\tps\nState:\tR (running)\nSlee"..., 1023) = 621
    close(6)
    So ps reads the information from files on proc filesystem ... maybe your program would better parse those files instead of ps. I'm sure you'll find any information you want in /proc The files there are well parseable, so why to waste your time with let's say rather complex output of ps.

    Have a nice day
    S.

  5. #5
    Just Joined!
    Join Date
    Aug 2007
    Posts
    3
    ok, the proc system is more reliable and contain a lot useful infomation.
    It's so good advice from you
    thank you!

  6. #6
    Just Joined! srerucha's Avatar
    Join Date
    Jun 2005
    Location
    Brno, Czech republic
    Posts
    58
    You're welcome, good luck

    S.

  7. #7
    Just Joined!
    Join Date
    Sep 2007
    Posts
    5
    Is it really necessary to use C.

    Have you tried piping the ps into a gawk script;
    really easy to pull fields off of a stream/file with gawk.

    The whole idea of UNIX/GNU is least effort, by processing plain text wherever possible.

    if you understand all this, then I apologize; sorry for the irrelevance.

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by blackguester View Post
    Hi guys,
    I have a problem when parsing the ps output data. Because there's no unique delimit between two fields(some field use spaces too, args for example), so I look through the man page to find how it can take a user-define delimit between two fields, but I find nothing.
    Someone has any ideas? thank you very much!
    you sure you looked? try this and see..delimiter is "|"
    Code:
    ps -eo "%p|%a"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...