home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / database / informix / 2849 < prev    next >
Encoding:
Text File  |  1992-12-31  |  6.4 KB  |  178 lines

  1. Path: sparky!uunet!olivea!gossip.pyramid.com!pyramid!infmx!cortesi
  2. From: cortesi@informix.com (David Cortesi)
  3. Newsgroups: comp.databases.informix
  4. Subject: making isql output horizontal
  5. Message-ID: <1992Dec31.210423.20141@informix.com>
  6. Date: 31 Dec 92 21:04:23 GMT
  7. Sender: news@informix.com (Usenet News)
  8. Organization: Informix Software, Inc.
  9. Lines: 167
  10.  
  11. Since I first posted this, at least two people have posted requests
  12. for solutions to the problem of "flattening" ISQL output.  For a long
  13. time I couldn't find this file, but finally it turned up.  So here it
  14. is, maybe Walt can stow it safely in his archive at Emory U?
  15.  
  16. #
  17. #    reform.isql.output.awk
  18. #
  19. #   An awk program to reformat an Informix-ISQL output listing,
  20. #   changing it from vertical format to horizontal report format.
  21. #
  22. #   Usage:
  23. #       gawk -f invsql [dpat="data-pattern"] [lpp=n] [hpat="head-pattern"]
  24. #
  25. #   <data-pattern> is a printf() pattern to format the column values
  26. #   <n> is the number of lines of data per page
  27. #   <head-pattern> is a printf() pattern to format the column headings
  28. #
  29. #   Headings are printed only when lpp is specified as greater than zero.
  30. #   See below for default heading and data patterns.
  31. #
  32. #   As written, supports only 30 columns of output. See the end of the
  33. #   program for how to expand this to more columns if required.
  34. #
  35. #   Requires an "awk" that matches the book by Aho et.al, that is,
  36. #   System V awk or Gnu awk -- not the obsolete awk shipped by Sun, NeXT, etc.
  37. #
  38. #   Author: David Cortesi (cortesi@informix.com)
  39. #
  40. # --------------------- User's Guide (wysiwig!) --------------------------
  41. #
  42. #   Standard input to invsql is an ISQL vertical-format report like this:
  43. #
  44. # order_num      1007
  45. # order_date     03/25/1989
  46. # customer_num   117
  47. # backlog        n
  48. # po_num         278693
  49. # ship_date      04/23/1989
  50. # ship_weight    125.90
  51. #
  52. #   We change it to horizontal format with optional page headings:
  53. #
  54. # order_num order_date customer_num backlog po_num  ship_date ship_weight
  55. #      1007 03/25/1989          117       n 278693 04/28/1989      125.90
  56. #      1012 06/05/1989          117       n 278701 06/09/1989       70.80
  57. #
  58. #   The program collects the column values from a group of input lines,
  59. #   then prints one output line using a printf() like this:
  60. #       printf(pattern,col1,col2,...,coln)
  61. #   where each "col" is the string value of that column from the input.
  62. #
  63. #   The default pattern is: "%nns %nns...\n" where each "nn" is the
  64. #   default width of that column, which is: the larger of the width of
  65. #   the heading text for that column, and the width of the data in that
  66. #   column in the very first input group.
  67. #
  68. #   The default is often wrong, but you can specify exact widths, and
  69. #   control the format in many other ways, by specifying a printf()
  70. #   pattern string as the command-line argument dpat="pattern".
  71. #
  72. #   The program can print column headings at the top of each page of
  73. #   data.  The default is to NOT print headings -- you can paginate
  74. #   the output using the pr(1) command for example.  However if you
  75. #   specify lpp=n, n>0, the program will print column headings before
  76. #   each group of n data lines.
  77. #
  78. #   The default column heading display is:
  79. #       printf("\f%nns %nns...\n\n",col1,col2...coln)
  80. #   where each "col" is the heading text of that column from the first
  81. #   input group, and the "nn" values are as for the data pattern.
  82. #   You can supply your own pattern using hpat="pattern" on the command line.
  83. #
  84. #   When writing printf patterns as part of c-shell commands you need
  85. #   only write the string in quotes, like this: dpat="%-5d\t%20f\n"
  86. #   (The c shell does not object to backslashes in such quotes.)
  87. #
  88. # --------------------- The Program  --------------------------
  89.  
  90. BEGIN { state = 0; colno = 0; recno = 0 }
  91.  
  92. # Leading blank lines: ignore them
  93.  
  94. state == 0 && $1 == "" { next }
  95.  
  96. # First data line of first group: note the length of the prefix
  97. # (the total width of heading and spaces), which is the same on each line,
  98. # even when the data value on a line is null.
  99.  
  100. state == 0 && $1 != "" {
  101.     match($0,/^[^ ]+ */)
  102.     pfxlen = RLENGTH+1
  103.     state = 1 # now into first data record
  104. }
  105.  
  106. # Any data line of any group: save the string value of the data line,
  107. # which is the whole line to the right of the prefix.  Data is saved
  108. # in the array v[1..n].
  109.  
  110. $1 != ""  {
  111.     v[++colno] = substr($0,pfxlen)
  112. }
  113.  
  114. # Any data line of the first data record: save the column name
  115. # as well, and set the default length of this column.  Column
  116. # names are saved in array h[1..n], and lengths in dlen[1..n].
  117.  
  118. state == 1 && $1 != "" {
  119.     h[colno] = $1
  120.     lh = length($1)
  121.     lv = length(v[colno])
  122.     dlen[colno] = (lh > lv)?lh:lv
  123. }
  124.  
  125. # End of first input group (empty line in state==1): build the
  126. # default print patterns, or use the supplied ones.
  127.  
  128. state == 1 && $1 == "" {
  129.     pl = 0  # no pagination
  130.     if (lpp > 0) pl = lpp
  131.     pd = ""
  132.     for( j = 1; j <= colno; ++j ) pd = pd "%" dlen[j] "s "
  133.     sub(" $","\n",pd)
  134.     ph = "\f" pd "\n"
  135.     if (dpat != "") pd = fixup(dpat)
  136.     if (hpat != "") {
  137.         ph = fixup(hpat)
  138.         if (pl == 0) pl = 60
  139.     }
  140.     if (pl > 0) recno = pl-1 # force starting headings
  141.     state = 2 # no more setting up to do
  142. }
  143.  
  144. # End of any input group (empty line): print.
  145.  
  146. $1 == "" {
  147. # the printf statements have to list every possible column 1..colno
  148. # Columns that do not exist generate no output (because there's no
  149. # format for them in the pattern :) and awk does not object to you
  150. # referring to empty array elements.  To support more columns, add
  151. # more lines to the printf statements below, following the same pattern.
  152.  
  153.     if ( ++recno == pl ) { # only succeeds when pl > 0
  154.         printf ph \
  155.             ,h[1],h[2],h[3],h[4],h[5],h[6],h[7],h[8],h[9],h[10] \
  156.             ,h[11],h[12],h[13],h[14],h[15],h[16],h[17],h[18],h[19],h[20] \
  157.             ,h[21],h[22],h[23],h[24],h[25],h[26],h[27],h[28],h[29],h[30]
  158.         recno = 0
  159.     }
  160.     printf pd \
  161.             ,v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10] \
  162.             ,v[11],v[12],v[13],v[14],v[15],v[16],v[17],v[18],v[19],v[20] \
  163.             ,v[21],v[22],v[23],v[24],v[25],v[26],v[27],v[28],v[29],v[30]
  164. }
  165.  
  166. # As received from the command line, the print patterns still have
  167. # literally "\n" instead of a newline. Convert the 5 possible format
  168. # effectors to the real things.
  169.  
  170. function fixup(pat) {
  171.     gsub("\\\\f","\f",pat)
  172.     gsub("\\\\n","\n",pat)
  173.     gsub("\\\\r","\r",pat)
  174.     gsub("\\\\t","\t",pat)
  175.     gsub("\\\\v","\v",pat)
  176.     return pat
  177. }
  178.