home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1997 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  6.3 KB

  1. From: lee@sq.sq.com (Liam R. E. Quin)
  2. Newsgroups: alt.sources
  3. Subject: tar2ls -- convert tar listings to ls -l format
  4. Message-ID: <1990Oct25.163639.12854@sq.sq.com>
  5. Date: 25 Oct 90 16:36:39 GMT
  6.  
  7. Two useful shell/nawk scripts...
  8.  
  9. I have a number of shell scripts that understand ls -l output, so the
  10. following shell script is useful to me because it turns a tar listing
  11. produced with tar tv into an ls -l format.
  12.  
  13. I've also enclosed a nawk-script that greatly abbreviates ls -l output,
  14. at the expense of losing greppability [sic!].  I posted lsabbrev once
  15. before, and some people liked it then....
  16.  
  17. Output from tar tv:
  18.     -rw-r--r-- lee/other 2123 Feb 14 22:47 1990 lee/samples/chapter1
  19.     -rw-r--r-- lee/other  549 Feb 14 22:47 1990 lee/samples/globe.map
  20.     -rw-r--r-- lee/other  2711 Jun 24 11:39 1988 lee/samples/SB/b.1
  21. Output from tar tv | tar2ls:
  22.     lee/samples:
  23.     -rw-r--r--   1 lee          2123 Feb 14 22:47 chapter1
  24.     -rw-r--r--   1 lee           549 Feb 14 22:47 globe.map
  25.  
  26.     lee/samples/SB:
  27.     -rw-r--r--   1 lee          2711 Jun 24  1988 b.1
  28. Output from tar tv | tar2ls | lsabbrev:
  29.     lee/samples:
  30.     Feb89 2.07K chapter1
  31.     Feb89  549b globe.map
  32.  
  33.     lee/samples/SB:
  34.     Jun88 2.65K b.1
  35.  
  36. Lee
  37. Liam R. E. Quin,  lee@sq.com, SoftQuad Inc., Toronto, +1 (416) 963-8337
  38.  
  39. : To unbundle, sh this file
  40. #! /bin/sh
  41. echo x - tar2ls 1>&2
  42. sed 's/^X//' >tar2ls <<'@@@End of tar2ls'
  43. X#! /bin/sh
  44. X
  45. X# Shell script to turn tar tv output into ls -l format
  46. X# Liam Quin, 1990
  47. X#
  48. X
  49. X# drwxr-xr-x lee/0      0 Apr 19 15:09 1989 4.3BSD-tahoe/
  50. X# drwxr-xr-x lee/0      0 Apr 13 16:28 1989 4.3BSD-tahoe/yawn.etc/
  51. X# -r--r--r-- lee/0   5760 Jun 18 17:35 1988 4.3BSD-tahoe/yawn.etc/vipw.c
  52. X# drwxr-xr-x lee/0      0 Apr 13 16:27 1989 4.3BSD-tahoe/yawn.etc/config/
  53. X
  54. X
  55. XYEAR=`date | awk '{print $6}` # newer systems have date +Y for this
  56. X                  # note that %y is different!
  57. X
  58. Xcat ${@+"$@"} | sed -e '
  59. X    s;^\(...........\)\([^/]*\)/\([^     ]*\)[    ][      ]*;\1 1 \2 \3 ;
  60. X    ' | nawk '
  61. XBEGIN { YEAR='${YEAR}'; }
  62. X
  63. X(NF < 9) {
  64. X    print # who knows what this is?
  65. X}
  66. X
  67. X{
  68. X    rwx = $1; links = $2; owner=$3; gid=$4; size=$5;
  69. X    month=$6; day=$7; time=$8
  70. X    year = $9
  71. X    name = $10
  72. X    for (i = 11; i <= NF; i++) {
  73. X    name = name " " $(i)
  74. X    }
  75. X}
  76. X
  77. X/^d/ || (name ~ /\/$/) {
  78. X    links = 2;
  79. X    next
  80. X}
  81. X
  82. X{
  83. X    ## be clever about directories:
  84. X    # find the parent directory
  85. X    parent = name
  86. X    sub(/\/[^/]*$/, "", parent)
  87. X    # if we are in a new directory, say so:
  88. X    if (parent != old) {
  89. X    printf "\n%s:\n", parent
  90. X    old = parent
  91. X    }
  92. X    gsub(/.*\//, "", name)
  93. X
  94. X    if (year == YEAR) {
  95. X    printf "%s %3d %-9s%8d %3s %2d %5s %s\n",
  96. X        rwx, links, owner, size, month, day, time, name
  97. X    } else {
  98. X    printf "%s %3d %-9s%8d %3s %2d %5s %s\n",
  99. X        rwx, links, owner, size, month, day, year, name
  100. X    }
  101. X}
  102. X    '
  103. @@@End of tar2ls
  104. echo x - lsabbrev 1>&2
  105. sed 's/^X//' >lsabbrev <<'@@@End of lsabbrev'
  106. X#! /bin/sh
  107. X
  108. X# take ls -lR format and reduce it somewhat...
  109. X# usage is ls -lR dir [...] | lsabbrev
  110. X# Liam Quin, 1989, 1990
  111. X
  112. X# remove the ^[d[r-] line if you want to see directories too
  113. X
  114. Xsed '
  115. X    /^d[r-][w-][xt-][r-][w-][xt-][r-][w-][xt-][ 0-9].* /d
  116. X    /^[dlbcp-][r-][^ ][^ ][r-][^ ][^ ][r-][^ ][xstS-] /s/^\(.\)........\(.\)/\1\2 /
  117. X' ${@+"$@"} |
  118. Xnawk '
  119. XBEGIN {
  120. X    group = 0 # 1 for ls -lg, 0 normally
  121. X    MEG = 1024.0 * 1024.0
  122. X    Descs["-"] = 1
  123. X    Fincr["-"] = 0 # no extra fields
  124. X    Descs["d"] = "(dir)"
  125. X    Fincr["d"] = 0 # no extra fields
  126. X    Descs["p"] = "FIFO:" # ugh, but want to stick to 5 chars
  127. X    Fincr["p"] = 0 # no extra fields
  128. X    Descs["b"] = "(bsp)"
  129. X    Fincr["b"] = 1 # major, minor gives 1 more field than "size" would
  130. X    Descs["c"] = "(csp)"
  131. X    Fincr["c"] = 1
  132. X    Descs["l"] = "(sym)"
  133. X    Fincr["l"] = 0 # no extra fields
  134. X    Descs["s"] = "(skt)" # Unix domain socket
  135. X    Fincr["s"] = 0 # no extra fields
  136. X}
  137. X
  138. X(NF >= 8 + group) {
  139. X    Type = substr($1, 1, 1)
  140. X    xf = 0 # extra fields
  141. X    xf = group
  142. X    if (Type in Descs) {
  143. X    if (Descs[Type] == 1) {
  144. X        filesz = mksz($(4 + xf)) # the size in bytes/k/m
  145. X    } else {
  146. X        filesz = Descs[Type]
  147. X    }
  148. X    xf = xf + Fincr[Type]
  149. X    } else {
  150. X    # a new type of special file probably
  151. X    filesz = "(?" Type "?)"
  152. X    }
  153. X
  154. X    printf "%s", $(5 + xf)
  155. X    Yr = 7 + xf
  156. X    if ($Yr ~ /19[0-9][0-9]/) {
  157. X    a = $Yr - 1900
  158. X    printf "%-2s %-5.5s", a, filesz
  159. X    } else {
  160. X        printf "89 %-5.5s", filesz
  161. X    }
  162. X    # sort of cope with spaces in fle names...
  163. X    # with BWKs awk, could simply delete fields 1 through 7, and
  164. X    # print what was left, but not with this old version of MKS awk
  165. X    for (i = 8 + Special; i <= NF; i++) { # circumvent bug in MKS awk
  166. X    printf " %s", $i
  167. X    }
  168. X    printf "\n"
  169. X    next
  170. X}
  171. X
  172. X/^total [0-9][0-9]*$/ { next }
  173. X
  174. X{
  175. X    # You might not want to do this
  176. X    sub(/\/usr\/spool\/ftp\//, "")
  177. X    print;
  178. X}
  179. X
  180. Xfunction mksz(s,  M, K, B)
  181. X{
  182. X    if (s < 1024) {
  183. X    return sprintf("%4db", s)
  184. X    } else if (s < 1024 * 10) {
  185. X    return sprintf("%-1.2fK", s / 1024.0)
  186. X    } else if (s < 1024 * 100) {
  187. X    return sprintf("%-2.1fK", s / 1024.0)
  188. X    } else if (s < 1024 * 1024) {
  189. X    return sprintf("%4dK", int( s / 1024.0))
  190. X    }
  191. X
  192. X    # assert: s >= MEG
  193. X    origs = s
  194. X
  195. X    m = (s * 1.0) / MEG
  196. X    M = int(s / MEG)
  197. X    s = s - (M * MEG)
  198. X    K = int(s / 1024)
  199. X    s = s - (K * 1024)
  200. X
  201. X    # assert: origs >= MEG
  202. X    # assert: s < 1024
  203. X
  204. X    if (M < 100) {
  205. X    if (M < 10) { # common case
  206. X        return sprintf("%-1.2fM", origs / MEG) # keep it as a float
  207. X    } else {
  208. X        return sprintf("%-2.1fM", origs / MEG) # keep it as a float
  209. X    }
  210. X    } else if (M < 1000) {
  211. X    return sprintf("%-4dM", M)
  212. X    } else {
  213. X    return "HUGE" # very unlikely...
  214. X    }
  215. X}
  216. X'
  217. X
  218. Xexit $?
  219. X
  220. X# total 52
  221. X# drwxrwxr-x  2 rph           512 Dec  7 17:02 1.5
  222. X# -rw-r--r--  1 rph         48479 Dec  7 16:58 129.97.129.116
  223. X# drwxrwxr-x  2 rph           512 Dec  7 17:12 2.0
  224. X# drwxrwxr-x  2 rph           512 Dec  7 17:11 2.2
  225. X# drwxrwxr-x  2 rph           512 Dec  7 17:04 2.3
  226. X
  227. X# total 210
  228. X# -rw-rw-r--   1 root     sys         2606 Sep 14 01:54 INTRO
  229. X 1           2 3      4     5       6    7 8    9
  230. X# l---------   1 root     sys            5 Sep 15 15:50 Index@ -> ls-lR
  231. X# -rw-r--r--   1 root     sys         1530 Nov  9 22:49 README.1st
  232. X# dr-xr-xr-x   2 root     sys          512 Sep 15 18:20 bin/
  233. @@@End of lsabbrev
  234. echo "You unpacked both files"
  235. exit 0
  236.  
  237.  
  238. -- 
  239. Liam R. E. Quin,  lee@sq.com, SoftQuad Inc., Toronto, +1 (416) 963-8337
  240.