a question regarding proper printf(3) formating and alignment
Karl Vogel
vogelke+unix at pobox.com
Wed Nov 17 22:14:11 UTC 2010
>> In the last episode (Nov 17), Alexander Best said:
A> i've looked at a lot of utilities in the bsd src tree and most of them
A> seem to be doing something like this:
A> Device 1M-blocks Used Avail Capacity
A> /dev/label/swapfs 10239 0 10239 0%
A> /dev/label/swap 8191 0 8191 0%
A> Total 18431 0 18431 0%
A> i'd like to learn of ways formatting the header so that it aligns
A> properly, whether the device name is 10 chars long or 1000.
ports/textproc/align is a nifty perl script that'll do this for just
about any type of column-based input. If you just want to fix "df" and
you know how long the longest device name is, try something like this:
#!/bin/ksh
#<df: human-readable aligned output from df
PATH=/bin:/usr/bin
BLOCKSIZE=1m
export PATH BLOCKSIZE
scr='
s/Available/Avail/
s/Capacity/Cap/
s/Mounted/Mount/
'
exec df ${1+"$@"} |
sed -e "$scr" |
awk '{printf "%-18s %12s %8s %8s %4s %s\n", $1,$2,$3,$4,$5,$6}'
exit 1
Comparison:
me% /bin/df /rd* /stage
Filesystem 1M-blocks Used Avail Capacity Mounted on
/dev/ad0s1f 224821 55691 151144 27% /rd01
/dev/ad1s1g 226611 34497 173985 17% /rd02
/dev/ad3s1a 263974 198167 44688 82% /rd03
/dev/ad3s1g 3962 174 3471 5% /stage
me% ./df /rd* /stage
Filesystem 1M-blocks Used Avail Cap Mount
/dev/ad0s1f 224821 55691 151144 27% /rd01
/dev/ad1s1g 226611 34497 173985 17% /rd02
/dev/ad3s1a 263974 198167 44688 82% /rd03
/dev/ad3s1g 3962 174 3471 5% /stage
>> On Wed, 17 Nov 2010 13:55:21 -0600,
>> Dan Nelson <dnelson at allantgroup.com> happily babbled in reply:
D> I seem to remember /usr/bin/find doing this dynamically by expanding
D> columns as it saw values that were larger than the previous max, but
D> apparently not (I just tested it).
I believe you're thinking of "file".
--
Karl Vogel I don't speak for the USAF or my company
When a man says it's a silly, childish game,
it's probably something his wife can beat him at. --Epperson's law
More information about the freebsd-questions
mailing list