Search code examples
c++pslsof

Alignment of columns in lsof and ps


I was working on parsing the output of popen to lsof and ps.

I was wondering is the first and last column always left aligned? And the rest in between are always right aligned? And as seen in column FD, is the minimum width of a column 3?

For example:

COMMAND PID  USER   FD   TYPE DEVICE  SIZE/OFF   NODE NAME\n
bash    252 noida  cwd    DIR    1,2      1088 410828 /Users/noida ion/Desktop\n
bash    252 noida  txt    REG    1,2    628736  11647 /bin/bash\n
bash    252 noida  txt    REG    1,2    622896  11866 /usr/lib/dyld\n
bash    252 noida  txt    REG    1,2 382100934 418209 /private/var/db/dyld/dyld_shared_cache_x86_64\n
bash    252 noida    0u   CHR   16,0  0t401079    601 /dev/ttys000\n
bash    252 noida    1u   CHR   16,0  0t401079    601 /dev/ttys000\n
bash    252 noida    2u   CHR   16,0  0t401079    601 /dev/ttys000\n
bash    252 noida  255u   CHR   16,0  0t401079    601 /dev/ttys000\n
0\u000b\u0001

Solution

  • The out put you are getting is may be a default formatted one. While passing the shell command in popen add the format options too.

    Pipe the column command to your lsof command.

    ex:

    to reverse the alignment

    lsof -ps | column -t | rev

    rough example:

    //for reversing the allignment.

    FILE *fp;
    char pOpenCmd[100] = "lsof -ps | column -t | rev";
    fp = popen(pOpenCmd, "r");
    

    for just aligning everything to left side, use "column -t"

    may be you can pipe "column -c" to specify width of columns.

    You have some more options here in this below link. http://man7.org/linux/man-pages/man1/column.1.html