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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!princeton!tex.Princeton.EDU!subbarao
  3. From: subbarao@tex.Princeton.EDU (Kartik Subbarao)
  4. Subject: Re: SED GURUS: Some Newline Questions
  5. Message-ID: <1992Sep11.023617.9472@Princeton.EDU>
  6. Sender: news@Princeton.EDU (USENET News System)
  7. Nntp-Posting-Host: tex.princeton.edu
  8. Organization: Princeton University
  9. References: <1992Sep10.230603.1267@ncsu.edu>
  10. Date: Fri, 11 Sep 1992 02:36:17 GMT
  11. Lines: 89
  12.  
  13. In article <1992Sep10.230603.1267@ncsu.edu> fester@mamushka.ncsu.edu (Cousin It) writes:
  14. >
  15. >Hi sed people.  For a project I'm working on, I'm trying to
  16. >figure out how to insert newlines in sed.  Man pages were
  17. >virtually useless, although I did determine that the "N"
  18. >might be necessary.  Here's the scenario:
  19.  
  20. Heh. Newlines with sed often pose problems. That's why I often say (and do)
  21. "Just use perl." But I'll bite. Just for the fun of it, I'll also give a perly 
  22. answer.
  23.  
  24. >1)  In the input text, lines ending with a semicolon need to have 
  25. >    a right bracket and newline inserted:
  26. >
  27. >--before--
  28. >start
  29. >{ THIS IS LINE 1;
  30. >{ THIS IS LINE 2;
  31. >end
  32. >
  33. >--after--
  34. >start
  35. >
  36. >{ THIS IS LINE 1; }
  37. >
  38. >{ THIS IS LINE 2; }
  39. >
  40. >end
  41. >
  42. >I've got this much:
  43. >
  44. >   sed -e 's/;$/; \}/'
  45. >                  ^necessary?
  46. >  
  47. >Adding \n, \\n, or \\\n was fruitless.
  48.  
  49. Remember, inserting a newline is as simple as hitting return :-)
  50.  
  51. For bourne-ish shells:
  52.  
  53. $ sed 's/;$/; }\
  54. /' filename
  55.  
  56. For the csh family: (Or like me, for zsh with cshjunkiequotes set)
  57.  
  58. % sed 's/;$/; }\\
  59. /' filename
  60.  
  61. The perlier version:
  62.  
  63. % perl -pe 's/;$/; }\n/' filename
  64.  
  65. >2)  Next, I need to delete a newline from text:
  66. >
  67. >--before--
  68. >start
  69. >TITLE 1 | |
  70. >subtopic
  71. >
  72. >end
  73. >
  74. >--after--
  75. >start
  76. >TITLE 1 | | subtopic
  77. >
  78. >end
  79. >
  80. >Any ideas?  
  81.  
  82. Easy in perl:
  83.  
  84. perl -pe 'chop if 'TITLE 1 \| \|/' filename
  85.  
  86. As to a sed solution, well, I'll defer to some sed expert who can post a
  87. cleaner version than what I would have posted. Just use perl.
  88.  
  89. I've tried rewriting the whole thing in awk and perl,
  90. >but it didn't work properly in awk, and I don't know perl well 
  91. >enough to get the thing to work in perl, although I did try the
  92. >s2p program.  Awk was a _lot_ easier to learn than perl, 
  93. >at least for me...
  94.  
  95. I'm not sure what the general form of 'TITLE 1 | |' is, but you could use
  96. regular expressions in perl to do that.
  97.  
  98. Aw, come on -- give perl a try. Sit down with the Camel book for an afternoon 
  99. and you'll soon be well on your way.
  100.  
  101.     -Kartik
  102.