lee@sq.sq.com (Liam R. E. Quin) (10/25/90)
Two useful shell/nawk scripts... I have a number of shell scripts that understand ls -l output, so the following shell script is useful to me because it turns a tar listing produced with tar tv into an ls -l format. I've also enclosed a nawk-script that greatly abbreviates ls -l output, at the expense of losing greppability [sic!]. I posted lsabbrev once before, and some people liked it then.... Output from tar tv: -rw-r--r-- lee/other 2123 Feb 14 22:47 1990 lee/samples/chapter1 -rw-r--r-- lee/other 549 Feb 14 22:47 1990 lee/samples/globe.map -rw-r--r-- lee/other 2711 Jun 24 11:39 1988 lee/samples/SB/b.1 Output from tar tv | tar2ls: lee/samples: -rw-r--r-- 1 lee 2123 Feb 14 22:47 chapter1 -rw-r--r-- 1 lee 549 Feb 14 22:47 globe.map lee/samples/SB: -rw-r--r-- 1 lee 2711 Jun 24 1988 b.1 Output from tar tv | tar2ls | lsabbrev: lee/samples: Feb89 2.07K chapter1 Feb89 549b globe.map lee/samples/SB: Jun88 2.65K b.1 Lee Liam R. E. Quin, lee@sq.com, SoftQuad Inc., Toronto, +1 (416) 963-8337 : To unbundle, sh this file #! /bin/sh echo x - tar2ls 1>&2 sed 's/^X//' >tar2ls <<'@@@End of tar2ls' X#! /bin/sh X X# Shell script to turn tar tv output into ls -l format X# Liam Quin, 1990 X# X X# drwxr-xr-x lee/0 0 Apr 19 15:09 1989 4.3BSD-tahoe/ X# drwxr-xr-x lee/0 0 Apr 13 16:28 1989 4.3BSD-tahoe/yawn.etc/ X# -r--r--r-- lee/0 5760 Jun 18 17:35 1988 4.3BSD-tahoe/yawn.etc/vipw.c X# drwxr-xr-x lee/0 0 Apr 13 16:27 1989 4.3BSD-tahoe/yawn.etc/config/ X X XYEAR=`date | awk '{print $6}` # newer systems have date +Y for this X # note that %y is different! X Xcat ${@+"$@"} | sed -e ' X s;^\(...........\)\([^/]*\)/\([^ ]*\)[ ][ ]*;\1 1 \2 \3 ; X ' | nawk ' XBEGIN { YEAR='${YEAR}'; } X X(NF < 9) { X print # who knows what this is? X} X X{ X rwx = $1; links = $2; owner=$3; gid=$4; size=$5; X month=$6; day=$7; time=$8 X year = $9 X name = $10 X for (i = 11; i <= NF; i++) { X name = name " " $(i) X } X} X X/^d/ || (name ~ /\/$/) { X links = 2; X next X} X X{ X ## be clever about directories: X # find the parent directory X parent = name X sub(/\/[^/]*$/, "", parent) X # if we are in a new directory, say so: X if (parent != old) { X printf "\n%s:\n", parent X old = parent X } X gsub(/.*\//, "", name) X X if (year == YEAR) { X printf "%s %3d %-9s%8d %3s %2d %5s %s\n", X rwx, links, owner, size, month, day, time, name X } else { X printf "%s %3d %-9s%8d %3s %2d %5s %s\n", X rwx, links, owner, size, month, day, year, name X } X} X ' @@@End of tar2ls echo x - lsabbrev 1>&2 sed 's/^X//' >lsabbrev <<'@@@End of lsabbrev' X#! /bin/sh X X# take ls -lR format and reduce it somewhat... X# usage is ls -lR dir [...] | lsabbrev X# Liam Quin, 1989, 1990 X X# remove the ^[d[r-] line if you want to see directories too X Xsed ' X /^d[r-][w-][xt-][r-][w-][xt-][r-][w-][xt-][ 0-9].* /d X /^[dlbcp-][r-][^ ][^ ][r-][^ ][^ ][r-][^ ][xstS-] /s/^\(.\)........\(.\)/\1\2 / X' ${@+"$@"} | Xnawk ' XBEGIN { X group = 0 # 1 for ls -lg, 0 normally 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 + group) { X Type = substr($1, 1, 1) X xf = 0 # extra fields X xf = group X if (Type in Descs) { X if (Descs[Type] == 1) { X filesz = mksz($(4 + xf)) # the size in bytes/k/m X } else { X filesz = Descs[Type] X } X xf = xf + Fincr[Type] X } else { X # a new type of special file probably X filesz = "(?" Type "?)" 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 # You might not want to do this X sub(/\/usr\/spool\/ftp\//, "") X print; X} X Xfunction 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 Xexit $? X X# total 52 X# drwxrwxr-x 2 rph 512 Dec 7 17:02 1.5 X# -rw-r--r-- 1 rph 48479 Dec 7 16:58 129.97.129.116 X# drwxrwxr-x 2 rph 512 Dec 7 17:12 2.0 X# drwxrwxr-x 2 rph 512 Dec 7 17:11 2.2 X# drwxrwxr-x 2 rph 512 Dec 7 17:04 2.3 X X# total 210 X# -rw-rw-r-- 1 root sys 2606 Sep 14 01:54 INTRO X 1 2 3 4 5 6 7 8 9 X# l--------- 1 root sys 5 Sep 15 15:50 Index@ -> ls-lR X# -rw-r--r-- 1 root sys 1530 Nov 9 22:49 README.1st X# dr-xr-xr-x 2 root sys 512 Sep 15 18:20 bin/ @@@End of lsabbrev echo "You unpacked both files" exit 0 -- Liam R. E. Quin, lee@sq.com, SoftQuad Inc., Toronto, +1 (416) 963-8337