home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9312 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  3.0 KB

  1. Path: sparky!uunet!mcsun!fuug!demos!kiae!glas!demos!bnrmtl.bnr.ca!pat
  2. From: pat@bnrmtl.bnr.ca
  3. Newsgroups: comp.unix.questions
  4. Date: 17 Jul 92 22:23 MDT
  5. Subject: Re: A Couple of Last Minute Odds 'n End
  6. Sender: Notesfile to Usenet Gateway <notes@glas.apc.org>
  7. Message-ID: <1992Jul17.182308.29660@bnrmtl.bn>
  8. References: <1992Jul17.120738.27558@ncsu.edu>
  9. Nf-ID: #R:1992Jul17.120738.27558@ncsu.edu:2114388534:1992Jul17.182308.29660@bnrmtl.bn:-1940795653:001:2538
  10. Nf-From: bnrmtl.bnr.ca!pat    Jul 17 22:23:00 1992
  11. Lines: 88
  12.  
  13.  
  14. [Sorry for the repost.  _Again_ I messed up the Distribution:]
  15.  
  16. In article <1992Jul17.120738.27558@ncsu.edu>, bregenma@gate.ncsu.edu (Bradley Regenman) writes:
  17. |    ... Now, however, if the next line is non-blank,
  18. |    it is to be assumed that it is a continuation.  In other
  19. |    words:
  20. |
  21. |    description of example | IOTA, KAPPA, LAMBDA, MU,
  22. |    NU, XI
  23. |
  24. |    another example description | OMICRON PI RHO SIGMA
  25. |    TAU
  26. |
  27. |    a shorter example description | TAU, UPSILON
  28. |
  29. |    Also, commas may or may not be used.  The only
  30. |    question I have is how to concatenate the next line to the
  31. |    previous one if it's non-blank.
  32.  
  33. How about this:
  34.  
  35.    > cat foo
  36.    description of example | IOTA, KAPPA, LAMBDA, MU,
  37.    NU, XI
  38.  
  39.    another example description | OMICRON PI RHO SIGMA
  40.    TAU
  41.  
  42.    a shorter example description | TAU, UPSILON
  43.  
  44.    > awk '{ printf "%s ", $0 } NF == 0 { print "\n" }' foo
  45.    description of example | IOTA, KAPPA, LAMBDA, MU, NU, XI
  46.  
  47.    another example description | OMICRON PI RHO SIGMA TAU
  48.  
  49.    a shorter example description | TAU, UPSILON
  50.  
  51.    >
  52.  
  53. It does add some unecessary blanks to the file, but that's
  54. probably not serious.
  55.  
  56.  
  57. |    sed "s/ALPHA/$alpha/" "$infile" |
  58. |    sed "s/BETA/$beta/"      |
  59. |    sed "s/DELTA/$delta/"    | 
  60. |    [ ~25 more ]
  61. |    sed "s/OMEGA/$omega/" > "$wkfile"
  62.  
  63. You only need one sed command to do all these changes:
  64.  
  65.    sed -e "s/ALPHA/$alpha/" \
  66.        -e "s/BETA/$beta/" \
  67.        ...
  68.        -e "s/OMEGA/$omega/" \
  69.        "$infile" > "$wkfile"
  70.  
  71. This should be somewhat faster than using multiple sed commands.
  72.  
  73.  
  74. |    So... I tried this (from Patrick Smith):
  75. |
  76. |      awk '{ for ( i = 1; i <= NF; i++ ) print NR, $i }' ${1+"$@"} \
  77. |      | sort +0n +1n \
  78. |      | awk 'NR != 1 && $1 != last { printf "\n" }
  79. |             $1 == last { printf ", " }
  80. |             { printf "%d", $2; last = $1 }
  81. |             END { printf "\n" }'
  82. |
  83. |    When I put it in a file by itself and ran it on test data, it ran
  84. |    correctly.  However, when I inserted it into my program, I got
  85. |    dozens of pages (literally) of '0,0,0,0' ... as output.  So...
  86.  
  87. Strange.  Did you forget to replace ${1+"@"} with the name of your
  88. input file?  (Or remove it completely if you're taking the input
  89. from a pipe.)
  90.  
  91. For faster solutions in awk and C, see another posting of mine.
  92. You may need to modify the programs slightly to handle blank
  93. lines in the input correctly.
  94.  
  95.  
  96. -- 
  97. Patrick Smith            preferred: larry.mcrcim.mcgill.edu!bnrmtl!pat
  98. Bell-Northern Research   next best: patrick@bnr.ca
  99. (514) 765-7914
  100.  
  101.