AloneInTheDark
AloneInTheDark

Reputation: 938

Setting column width of a command output on AIX

I'm trying to parse df -Pg command's output in my Java application, which will print for me as i want.

Here is the output of the command:

Filesystem    GB blocks      Used Available Capacity Mounted on
/dev/hd4           1.00      0.30      0.70      31% /
/dev/hd2           3.62      2.65      0.98      74% /usr
/dev/hd9var        2.00      0.19      1.81      10% /var
/dev/hd3           2.88      1.61      1.27      56% /tmp
/dev/hd1           2.00      1.30      0.70      66% /home
/proc                 -         -         -       -  /proc
/dev/hd10opt       1.00      0.27      0.73      28% /opt
/dev/fslv00        2.88      1.25      1.63      44% /tsmfiles
/dev/fslv01        2.00      0.84      1.16      43% /opt/IBM/ITM
/dev/livedump      0.25      0.00      0.25       1% /var/adm/ras/livedump
/dev/DoOnceAIX      0.75      0.00      0.75       1% /DoOnceAIX
/dev/nmonlv        1.00      0.03      0.97       4% /var/perf/poa
/dev/fslv02       49.50     42.86      6.64      87% /u01
/dev/fslv03      685.00    312.62    372.38      46% /u02
akslltsm:/depot     60.50     43.06     17.44      72% /mnt

My parser in java splits this text with spacing size. As it read lines, parser splits the string when hee sees at least 1 space character. As you can see, there are some lines inside the column (like "GB blocks" and "Mounted on") so i can't split as i want.

What i want is to set a column width (2+ space characters) and get my program to parse it successfully.

Upvotes: 0

Views: 494

Answers (1)

pedz
pedz

Reputation: 2349

Catch the special cases and fill in the spaces, adjust the columns, then undo what you did in the first step.

e.g.

step one: sed -e 's/GB Blocks/GB_Blocks/' -e 's/Mounted on/Mounted_on/'

second step: your normal program that adjusts the columns

third step: sed -e 's/GB_Blocks/GB Blocks/' -e 's/Mounted_on/Mounted on/'

Then just pipe them together:

sed -e ... | <your prog> | sed -e ...

This is a fairly general technique for this type of situation. If the input isn't able to be parsed (using whatever definition you need at the time), then alter the input to work, parse it, then reverse the first alteration.

Upvotes: 1

Related Questions