home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / shell / 5086 < prev    next >
Encoding:
Internet Message Format  |  1992-12-16  |  2.2 KB

  1. Path: sparky!uunet!spool.mu.edu!uwm.edu!psuvax1!ukma!cs.widener.edu!dsinc!phent!kkress
  2. From: kkress@phent.UUCP (Kenneth R. Kress)
  3. Newsgroups: comp.unix.shell
  4. Distribution: world
  5. Subject: Re: Substituing using sed, ...
  6. Keywords: sed awk substituting
  7. Message-ID: <9212158153@phent.UUCP>
  8. Organization: That's Entertainment BBS
  9. Date: Tue, 15 Dec 92 22:06:54 EST
  10. Lines: 63
  11.  
  12. Uwe Waldmann writes:
  13. > In article <1992Dec14.042422.22897@candle.uucp>, root@candle.uucp
  14. > (Bruce Momjian) writes:
  15. > > Here is a sample file:
  16. > > 
  17. > >     acaX csacacXcascsc  
  18. > >     "cscaXcacsXcs"  csaXc "X" caX
  19. > >     Xaca Xcaca  ca "caXcas"  ccaX cacs
  20. > > 
  21. > > I want to use sed to replace X with another character, but only if the X
  22. > > does not appear within quotes.  The quoted strings do not extend across
  23. > > new-lines.
  24. > That's easy:
  25. > #!/bin/sh
  26. > sed -e '
  27. > s/^/\
  28. > /
  29. > : a
  30. > s/\(\n[^"]*\)X/\1Y/
  31. > t a
  32. > /\n.*".*"/ {
  33. > s/\n\([^"]*\)"\([^"]*\)"/\1"\2"\
  34. > /
  35. > t a
  36. > }
  37. > s/\n//
  38. > ' ${1+"$@"}
  39. Uwe Waldmann,
  40.     I was unable to test your solution because my version of sed
  41. gave me pattern overflow errors. And I was unable to decipher your
  42. technique. But trying to understand it suggested several other
  43. strategies.
  44.     If Bruce Momjian doesn't mind deconstructing his original file,
  45. one could do:
  46.     tr -s ' ' '\n' datafile        |    # every word to a line
  47.     sed 's/    //'             |    # remove tabs
  48.     sed '/^[^"]/s/X/Y/g'             # if line has no quote
  49.                         # do the translation
  50.     And this unsatisfactory solution led me to the solution I 
  51. should have thought of first:
  52. awk '
  53. {    # do this for every line
  54.     for (i = 1; i <= NF; i++) {        # loop through words on line
  55.         if ( $i !~ /\"/) {        # not quoted
  56. #            gsub (/X/,"Y",$i )    #   substitute Y for X
  57.             printf ("%s", $i )     #   print out word
  58.         } else {            # else
  59.             printf ("%s", $i )    #   print out word
  60.         }
  61.     }                    # line is done, so
  62.     printf "\n"                # print newline
  63. }'
  64.     This requires nawk or gawk.
  65.                 Ken.
  66. ===================================================================
  67. Ken Kress, Glenside, PA * dsinc!phent!kkress 
  68.     Daring ideas are like chessmen moved forward;
  69.     they may be beaten, but they may start a winning game.
  70.                     Johann Wolfgang von Goethe
  71. -------------------------------------------------------------------
  72.