home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 11006 < prev    next >
Encoding:
Text File  |  1992-09-12  |  2.7 KB  |  104 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!nucsrl!ddsw1!dattier
  3. From: dattier@ddsw1.mcs.com (David W. Tamkin)
  4. Subject: Re: SED GURUS: Some Newline Questions
  5. Message-ID: <BuHy8M.3EA@ddsw1.mcs.com>
  6. Keywords: sed newline
  7. Organization: Contributor Account at ddsw1, Chicago, Illinois  60657
  8. References: <1992Sep10.230603.1267@ncsu.edu>
  9. Date: Sun, 13 Sep 1992 03:12:21 GMT
  10. Lines: 92
  11.  
  12. fester@mamushka.ncsu.edu wrote in <1992Sep10.230603.1267@ncsu.edu>:
  13.  
  14. | Hi sed people.  For a project I'm working on, I'm trying to
  15. | figure out how to insert newlines in sed.  Here's the scenario:
  16. | 1)  In the input text, lines ending with a semicolon need to have 
  17. |     a right bracket and newline inserted:
  18.  
  19. What you show there are braces; the right bracket is "]".
  20.  
  21. | --before--
  22. | start
  23. | { THIS IS LINE 1;
  24. | { THIS IS LINE 2;
  25. | end
  26. | --after--
  27. | start
  28. | { THIS IS LINE 1; }
  29. | { THIS IS LINE 2; }
  30. | end
  31. | I've got this much:
  32. |    sed -e 's/;$/; \}/'
  33. |                   ^necessary?
  34. |   
  35. | Adding \n, \\n, or \\\n was fruitless.
  36.  
  37. You're adding a space and a right brace there.  (No, the right brace doesn't
  38. need escaping inside a text string.)  Do you want a space as well?  I'll
  39. assume not.
  40.  
  41. There are at least four ways to go:
  42.  
  43. # This assumes that the hold space, which starts out empty, will stay empty:
  44. sed '/;$/{
  45. G
  46. s/$/}/
  47. }'
  48.  
  49. [Append a newline and the hold space and then place a right brace at the
  50. right anchor.]
  51.  
  52. # This one must be strong-quoted or placed in a script file for sed -f:
  53. sed 's/;$/&\
  54. /'
  55.  
  56. [(Well, it could work weak-quoted if you use two backslashes.)  Place a
  57. newline and a right brace at the right anchor.]
  58.  
  59. # The same cautions to protect the trailing backslash apply to this one:
  60. sed '/;$/a\
  61. }'
  62. [Output a line consisting of a right brace after you finish with the line
  63. being processed.]
  64.  
  65. # If you don't need the hold space for anything else, keep a brace there:
  66. sed '1{
  67. h
  68. s/.*/}/
  69. x
  70. }
  71. /;$/G'
  72. [Put a right brace into the hold space and append it (with a separating
  73. newline) to every line otherwise ending in a semicolon.]
  74.  
  75. | 2)  Next, I need to delete a newline from text:
  76.  
  77. You need to use N to get both lines (with a separating embedded newline) into
  78. the pattern space at once: it's all in The Ferkakte Manual.  Then you can
  79. search for an embedded newline with \n (unfortunately, \n doesn't work in
  80. replacement strings).
  81.  
  82. To change every occurrence of 
  83.  
  84. pattern1             to          pattern1 pattern2
  85. pattern2
  86.  
  87. you can do something like this (just trust me):
  88.  
  89. sed 'N
  90. s/\(pattern1\)\n\(pattern2\)/\1 \2/
  91. $ b
  92. P
  93. D'
  94.  
  95. David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297
  96. dattier@ddsw1.mcs.com    CompuServe: 73720,1570    MCI Mail: 426-1818
  97.