So the challenge is:
Replicate the output of a du or df with the -h flag. Example:
Replicate it in such a way that it can be passed input from standard in.minotaur% df -k | grep c0t1 /dev/dsk/c0t1d0s1 1269615 814811 391324 68% /usr /dev/dsk/c0t1d0s3 245407 44583 176284 21% /var /dev/dsk/c0t1d0s0 424519 187200 194868 49% /home minotaur% df -h | grep c0t1 /dev/dsk/c0t1d0s1 1.2G 796M 382M 68% /usr /dev/dsk/c0t1d0s3 240M 44M 172M 21% /var /dev/dsk/c0t1d0s0 415M 183M 190M 49% /home
Special note: if you can do it in sed or some other shell command (ideally a common one, let's not be linux or solaris or bsd centric here), that is fine, given the intent is to have a working shell alias. Contestants will be judged from the first character after the pipe:
alias du 'du | perl ... '
So, yeah, that means you get a 1 character bonus for using sed, but I doubt your command would be as compact.
Consider it a Unix "brown bag" golf. Enjoy!
brother deprecated
--
Laziness, Impatience, Hubris, and Generosity.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A little golf anyone?
by jsprat (Curate) on Oct 30, 2002 at 22:23 UTC | |
Output using the sample data looks like this:
I didn't come up with a way to cleanly format G to 1 decimal place, leaving K and M at a whole number (ie 1.2G, 240M). 94 if less precision and un-aligned formatting meets your standards :-P
Update: fixed miscounts | [reply] [d/l] [select] |
Re: A little golf anyone?
by petral (Curate) on Oct 31, 2002 at 00:00 UTC | |
but it does produce: from your sample (note truncation instead of rounding). Maybe I can think of something more sensible in the morning. upday:   Ok, this is simpler (102 chars): It still preserves numeric alignment with the original text, eg, and rounding adds 22 (to 124 chars): Update, the second: Here's a funny format one at 79 chars: which produces this: And an extendible one at 114 chars (got to be ready for those terrabyte-sized disks): Which does: Ok, here's a real one at 117 chars that does 1024-type m's and g's and t's: Which gets: This ignores several problems:  1) Before 5.6, perl expanded `qw(...)' into `split" ",'...'' (so you couldn't [index]it );  2) There's no protection against filenames or whatever which end with a string of 4 digits;  and 3) There's no provision for catching a number at the end of the line. Adding these back in, yields 132 chars:
(BTW, what comes after Terrabytes?) Update the last:   Updated the above to save a few strokes (and round properly on the 1024 one (changed %d -> %.0f)).   p | [reply] [d/l] [select] |
by duelafn (Parson) on Oct 31, 2002 at 04:06 UTC | |
If we didn't reinvent the wheel, we wouldn't have rollerblades. | [reply] [d/l] |
Re: A little golf anyone?
by Aristotle (Chancellor) on Oct 31, 2002 at 00:56 UTC | |
Makeshifts last the longest. | [reply] [d/l] [select] |
Re: A little golf anyone?
by robartes (Priest) on Oct 30, 2002 at 20:59 UTC | |
CU Update: Killed two spaces. | [reply] [d/l] |
Re: A little golf anyone?
by sauoq (Abbot) on Oct 31, 2002 at 02:34 UTC | |
Why go to all that trouble? The GNU fileutils package compiles neatly on Solaris 8. -sauoq "My two cents aren't worth a dime."; | [reply] |
by Aristotle (Chancellor) on Oct 31, 2002 at 07:37 UTC | |
Makeshifts last the longest. | [reply] |
by deprecated (Priest) on Nov 01, 2002 at 14:53 UTC | |
The fact of the matter is, I'm trying to apply a global configuration to a lot of machines, most of which do not have more than 540mb of internal disk space. These machines have perl. They do not have gcc or forte, and it would take a long time to compile these packages, and it simply isnt reasonable to go and install a package to do that. It is much easier and practical to insert a du | perlism into my .profile. I wanted also to say that this reminds me a lot of the typical irc #perl channels. this kind of response is just rampant on IRC and on mailing lists, and I even see it here. It bothers me because these answers pollute search engines like Google. When somebody is searching for an answer on how to, say, format a file into six columns with perl, they're going to find ten answers from the chanop/listmaven type saying "just use fmt(1)rather than explaining what they need to do." Far more effective than telling somebody something else they can do is to answer the question and point them in another direction that would help. I've bitched about this in the past, but its been a year or so, so I'm inclined to bring it up again. brother dep | [reply] [d/l] |
by sauoq (Abbot) on Nov 02, 2002 at 00:48 UTC | |
You forgot the rest of that irc conversation: At least, that's how it used to be on OPN/#perl when it was still relatively small and friendly. So, you were bothered by my reply. I'm left wondering why you were bothered that I asked: "Why go to all that trouble?" and then added "The GNU fileutils package compiles neatly on Solaris 8." as an explanation. Perhaps you feel underestimated. Perhaps you think that when you ask a question I should assume that you know what you are doing and not question your enlightened approach. You might even be right but its a rather ungracious attitude to have when you are seeking help. You complained about the answer I gave you when, in fact, my response wasn't intended to be an answer at all, but a probe for more information. Since you seem taken aback by it, I'll explain why I questioned you. In your original post you wrote: (the principle being, if youre going to do it a bunch of times, you want it to be fast) You want it to be fast. Fine, we'll ignore that you want to start up a perl interpreter for this... But why ask for a golfed solution? Short doesn't mean fast and the shortest solution for many problems is far slower than the fastest. Asking for the two things together is kind of, uh, green. Because you want it to be part of a shell alias isn't, by itself, a good justification for a golfed solution either. Shortish may be nice, maybe, but why golfed? Admittedly, it's ugly in tcsh but if you are using tcsh you are probably used to doing ugly things similar to:
If, on the other hand, you did decide to use GNU du/df binaries, they weigh in at about 45k (stripped) each. I don't think they rely on special libraries, so you wouldn't have to install a package, you'd probably just have to copy the binaries around. (If you really have a lot of machines, NFS would probably make life easier.) Not only are they fast, they provide additional functionality and unlike shell aliases, they can be reliably called from scripts. Shell aliases are useful for many things but I wouldn't use one for this because it seems to me like too much complexity for too little gain. I would either use the GNU versions or get used to reading long numbers. (In fact, I have many gnu tools in my ~/bin which is automounted on most of the machines I work on but I tend to just use -k with df/du even though our arrays have 150Gb+. It comes pretty easily after a bit.) If you really insist on using aliases, I recommend against calling them "du" or "df" because you'll get frustrated all over again when you regularly find the need to use the commands with other switches like df -e or du -L. Back to the point... In your sig, you list "Generosity" along with the three great virtues of a programmer. I don't know that it really takes generosity to be a great programmer but it is certainly a quality worth having. Another one is humility. Humility enables us to truly accept the generosity of others. -sauoq "My two cents aren't worth a dime."; | [reply] [d/l] [select] |
Wildly over-engineered solution to df formatting
by blssu (Pilgrim) on Oct 31, 2002 at 18:17 UTC | |
Ok, this isn't golfing, but it's kinda cool anyways. I thought it would be nice to have a generalized number formatter that automagically recognized columns of numbers and reformatted then to KB, MB, etc. (BTW, the systems that inter-mix unit scales are very unfriendly. Outlook does this for example -- it intermixes bytes, kb and mb -- which makes it almost impossible to see relative sizes by quickly scanning a list.) Here are a few examples of the automagical number formatter:
Here's the Foo.pm module itself. It's too long and messy. Sorry about that. It's also not smart enough to handle columns of numbers that don't quite line up. (OT: Wouldn't it be fun if all our tools could generate XML? And our terminals could automatically format it?)
| [reply] [d/l] [select] |
by petral (Curate) on Nov 06, 2002 at 15:11 UTC | |
Thank you.   p | [reply] |
Re: A little golf anyone?
by petral (Curate) on Nov 07, 2002 at 19:55 UTC | |
which is 104 chars not counting the decorative white space.   p | [reply] [d/l] |