Newsgroups: alt.sources Path: utzoo!sq!lee From: lee@sq.sq.com (Liam R. E. Quin) Subject: ls -lR summary generator (nawk & sed) Message-ID: <1989Dec2.015928.3612@sq.sq.com> Reply-To: lee@sq.com (Liam R. E. Quin) Organization: Unixsys (UK) Ltd Distribution: alt Date: Sat, 2 Dec 89 01:59:28 GMT 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 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! 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 :-( Anyway, here is lsabbrev. Lee #! /bin/sh echo shar: Extracting \"'lsabbrev'\" \(2546 characters\) sed "s/^X//" >'lsabbrev' <<'END_OF_FILE' X#! /bin/sh X X# take ls -lR format and reduce it somewhat... X# usage is ls -lR dir [...] | % X# where % is the name of this file X X# remove the ^[d[r-] line if you want to see directories too X sed ' X /^d[r-][w-][xt-][r-][w-][xt-][r-][w-][xt-][ 0-9].* /d X /^[d-]........../s/^\(.\)........\(.\)/\1\2 / X' ${@+"$@"} | nawk ' BEGIN { X MEG = 1024.0 * 1024.0 X Descs["-"] = 1 X Fincr["-"] = 0 # no extra fields X Descs["d"] = "(dir)" X Fincr["d"] = 0 # no extra fields X Descs["p"] = "FIFO:" # ugh, but want to stick to 5 chars X Fincr["p"] = 0 # no extra fields X Descs["b"] = "(bsp)" X Fincr["b"] = 1 # major, minor gives 1 more field than "size" would X Descs["c"] = "(csp)" X Fincr["c"] = 1 X Descs["l"] = "(sym)" X Fincr["l"] = 0 # no extra fields X Descs["s"] = "(skt)" # Unix domain socket X Fincr["s"] = 0 # no extra fields X} X X(NF >= 8) { X Type = substr($1, 1, 1) X xf = 0 # extra fields X if (Type in Descs) { X if (Descs[Type] == 1) { X filesz = mksz($4) # the size in bytes/k/m X } else { X filesz = Descs[Type] X } X xf = Fincr[Type] X } else { X # a new type of special file probably X filesz = "(?" Type "?)" X xf = 0 # who knows? X } X X printf "%s", $(5 + xf) X Yr = 7 + xf X if ($Yr ~ /19[0-9][0-9]/) { X a = $Yr - 1900 X printf "%-2s %-5.5s", a, filesz X } else { X printf "89 %-5.5s", filesz X } X # sort of cope with spaces in fle names... X # with BWKs awk, could simply delete fields 1 through 7, and X # print what was left, but not with this old version of MKS awk X for (i = 8 + Special; i <= NF; i++) { # circumvent bug in MKS awk X printf " %s", $i X } X printf "\n" X next X} X X/^total [0-9][0-9]*$/ { next } X X{ X sub(/\/usr\/spool\/ftp\//, "") X print; X} X function mksz(s, M, K, B) X{ X if (s < 1024) { X return sprintf("%4db", s) X } else if (s < 1024 * 10) { X return sprintf("%-1.2fK", s / 1024.0) X } else if (s < 1024 * 100) { X return sprintf("%-2.1fK", s / 1024.0) X } else if (s < 1024 * 1024) { X return sprintf("%4dK", int( s / 1024.0)) X } X X # assert: s >= MEG X origs = s X X m = (s * 1.0) / MEG X M = int(s / MEG) X s = s - (M * MEG) X K = int(s / 1024) X s = s - (K * 1024) X X # assert: origs >= MEG X # assert: s < 1024 X X if (M < 100) { X if (M < 10) { # common case X return sprintf("%-1.2fM", origs / MEG) # keep it as a float X } else { X return sprintf("%-2.1fM", origs / MEG) # keep it as a float X } X } else if (M < 1000) { X return sprintf("%-4dM", M) X } else { X return "HUGE" # very unlikely... X } X} X' X END_OF_FILE if test 2546 -ne `wc -c <'lsabbrev'`; then echo shar: \"'lsabbrev'\" unpacked with wrong size! fi chmod +x 'lsabbrev' exit 0 -- Liam R. Quin, Unixsys (UK) Ltd [note: not an employee of "sq" - a visitor!] lee@sq.com (Whilst visiting Canada from England, until Christmas) utai!anduk.uucp!lee (after Christmas) ...striving to promote the interproduction of epimorphistic conformability Brought to you by Super Global Mega Corp .com