Date: Wed, 17 Nov 2010 16:44:33 -0500 (EST) From: vogelke+unix@pobox.com (Karl Vogel) To: freebsd-questions@freebsd.org Subject: Re: a question regarding proper printf(3) formating and alignment Message-ID: <20101117214433.9D0DBBF95@kev.msw.wpafb.af.mil> In-Reply-To: <20101117195521.GF57869@dan.emsphone.com> (message from Dan Nelson on Wed, 17 Nov 2010 13:55:21 -0600) References: <20101117155006.GA88394@freebsd.org> <20101117195521.GF57869@dan.emsphone.com>
next in thread | previous in thread | raw e-mail | index | archive | help
>> 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@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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20101117214433.9D0DBBF95>