ls -lR summary generator (nawk & sed)

Tom Christiansen tchrist at convex.COM
Sun Dec 3 02:02:05 AEST 1989


I thought a while before posting this.  I don't want to detract from 
ANYONE posting sources.  I think alt.sources is wonderful because
anyone can post whatever little tool they've been using to make their
lives easier.  I want to thank Liam R.E. Quin for his posting.  I 
think that the scaling on sizes (b, K, M) is interesting.

I do have some questions though.
  
In article <1989Dec2.015928.3612 at sq.sq.com> lee at sq.com (Liam R. E. Quin) writes:
|THe enclosed little shell script uses sed and nawk to take the output of
|ls -l or ls -lR (sysV or BSD) and produce output that looks like
|Sep86 42.0K arp
|Sep86 16.4K biod
|Sep86 45.3K chown
|Dec87 (sym) config -> /usr/etc/config
|Sep86 24.3K cron

Let's see I've I understand this -- you're just rearranging ls -l output,
right?  You're squeezing the date and throwing out some other columns.
And are sizes the sizes really accurate?  I usually prefer to know 
blocks used then sizes by byte, for which you'd want the 'ls -sl'
output.  Otherwise you'll be charged for the holes in the files.
Dbm files are a good example of files with holes in them.

|which can then be printed in multiple columns (for example).
|
|It's an example of using awk to post-process ls output, something I seem
|to do once every couple of years...  Please, no perl one-liner equivalents!

How come?  Nawk isn't available via anonymous FTP or automatic UUCP
as perl is.  I would be willing to bet that more people can get 
perl than nawk, since nawk isn't free.  And the author himself admits in 
comments that there are various version of nawk, some of which would
make certain constructs work, others which would not.

Now I don't really think I could do it in just one line of perl, unless I
trimmed newlines :-).  I'd have to play with the date depending on ls's
output.  Or I could do the stats that 'ls -l' does myself and not worry.
But anyway, I'm pretty sure it would be substantially smaller than the
version.  If I really wanted to duplicate lsabbrev in perl, I might use
the a2p and s2p translators and work from there, although rewriting by
hand usually yields tighter code.

But probably for my purposes I'd just do this and skip awk and perl both:

ls -F -s -l | sed -n -e 's/\(....\).................................\(............\).\(.*\)/\1 \2 \3/p' | sort +0rn -1

yielding output beginning like this (from /etc):

 192 Oct  3 11:20 rpc.lockd*
 176 Oct 11  1988 restore*
 152 Oct 11  1988 xdump*
 144 Oct 11  1988 dump*
 144 Oct 11  1988 op*
 136 Nov 16  1987 timedc*
 136 Oct 11  1988 fsck*
 136 Oct 11  1988 inetd*
 128 Apr 11  1989 timed*
 128 Oct 11  1988 cron*

All sizes are in actual kilobytes used by the file.

If I wanted to muck with the dates, I'd probably put the whole
thing into perl rather than trying to figure out the logic in 
sed.  Plus it's hard to get the current year in sed.

|I also have a version of pr (sort of) that works out how many columns
|to use on a per-page basis, with which this is very effective.  Mail me
|if you want it -- it was written by a friend and posted (as "pf") to
|net.sources in 1984 I think.  Saves lots of paper, & good for C where
|the single brace at the end of the file might otherwise have been on a
|page by itself :-(

I posted a similar tool in perl to alt.sources a while back called "words".
This kind of thing does save paper.  And yes, mail me if you want it.

--tom

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist at convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"



More information about the Alt.sources.d mailing list