Greg Wooledge <g...@wooledge.org> writes:

> On Sun, Feb 19, 2023 at 12:04:22PM -0600, David Wright wrote:
>> But even that's not enough
>> because the field width is somewhat variable: try   ps -eo '%c  |  %z  |  %a'
>> (We can still use | to make the problem somewhat more obvious.)
>
> Oh wow.  Yeah, OK, that's not really solvable.
>
> For those who don't want to try to reverse engineer David's conclusion,
> or who don't just happen to stumble upon it with their current process
> list, here's what I'm seeing:
>
> COMMAND          |     VSZ  |  COMMAND
> systemd          |  164140  |  /sbin/init
> kthreadd         |       0  |  [kthreadd]
> rcu_gp           |       0  |  [rcu_gp]
> rcu_par_gp       |       0  |  [rcu_par_gp]
> [...]
> steamwebhelper   |  4631064  |  /home/greg/.steam/debian-installation/[...]
> [...]
> chrome_crashpad  |  33567792  |  
> /opt/google/chrome/chrome_crashpad_handler[...]
> [...]
> kworker/3:0-eve  |       0  |  [kworker/3:0-events]
>
> ps appears to guess an initial maximum width for the VSZ field, but
> when a value comes along that exceeds the guessed maximum, it simply
> shoves the field barrier over.  It doesn't even become the new maximum,
> with all of the fields aligning after that.  It's just a one-time shove,
> breaking the current line only.
>
> Therefore, parsing the header line cannot give us enough information to
> insert field separators correctly in body lines after the fact.


Dear all,

Thanks for chiming in.  The example was indeed simplified and I am using
%a which can contain internal whitespace.

This is the command I was using previously:

  ps -eo '%p|%c|%C' -o "%mem" -o '|%a' --sort=-%cpu

I now replaced it with

  ps -eo '%p %c %C' -o "%mem" -o ' %a' --sort=-%cpu  | sed -E 's/([0-9]+) (.+) 
([0-9]+.?[0-9]?) ([0-9]+.?[0-9]?) (.+)/\1|\2|\3|\4|\5/'
 
This works, but is of course cumbersome to maintain.

Again, thanks for all the comments!

Best,
Andreas

Reply via email to