home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 10987 < prev    next >
Encoding:
Internet Message Format  |  1992-09-12  |  2.3 KB

  1. Path: sparky!uunet!sun-barr!ames!lll-winken!iggy.GW.Vitalink.COM!cs.widener.edu!dsinc!phent!phent!kkress
  2. From: kkress@phent.UUCP (Kenneth R. Kress)
  3. Newsgroups: comp.unix.questions
  4. Subject: sed and linefeeds
  5. Message-ID: <3178255@phent.UUCP>
  6. Date: 12 Sep 92 05:53:28 GMT
  7. Sender: news@phent.UUCP (GNEWS Version 2.0 news poster.)
  8. Organization: That's Entertainment BBS -- Glenside, Pa.
  9. Lines: 70
  10.  
  11. fester@mamushka.ncsu.edu (Cousin It) writes:
  12. > Date: Thu, 10 Sep 1992 23:06:03 GMT
  13. > For a project I'm working on, I'm trying to
  14. > figure out how to insert newlines in sed. ...
  15. > although I did determine that the "N"
  16. > might be necessary.  Here's the scenario:
  17. > 1)  In the input text, lines ending with a semicolon need to have 
  18. >     a right bracket and newline inserted:
  19. > --before--
  20. > start
  21. > { THIS IS LINE 1;
  22. > { THIS IS LINE 2;
  23. > end
  24. > --after--
  25. > start
  26. > { THIS IS LINE 1; }
  27. > { THIS IS LINE 2; }
  28. > end
  29. > 2)  Next, I need to delete a newline from text:
  30. > --before--
  31. > start
  32. > TITLE 1 | |
  33. > subtopic
  34. > end
  35. > --after--
  36. > start
  37. > TITLE 1 | | subtopic
  38. > end
  39. > Any ideas? 
  40.   
  41.  I don't pretend to be an expert, but I came up with a shell
  42.  script using sed and tr that does what you say you want.
  43.   It requires you be able to pick a symbol that is not
  44.  anywhere in your document. I used the at sign in my script.
  45.  
  46.  The script:
  47.  
  48.  sed 's/\;$/\; \}\@/
  49.      /\| \|/N
  50.      s/\n/ /' |
  51.  tr "@" "\n"
  52.  
  53.   tr turns the at signs into new lines (I couldn't get
  54.  sed to do it either, although I'm pretty sure the MKS sed
  55.  I have at work will insert newlines.)
  56.   As you guessed, the N is needed to put both lines into 
  57.  the work area and the substitute command replaces the newline
  58.  with a space.
  59.   An awk script to do the same chores:
  60.  
  61. awk '{        
  62.     if ($0 ~ /;$/) {        # if the line ends with semi-colon
  63.         print $0" \}\n";    # append bracket and linefeed
  64.         next }            # fetch next input line
  65.     if ($0 ~ /\| \|/) {        # if line contains | symbols
  66.         printf ("%s ",$0);    # print without a linefeed
  67.         next }            # fetch next input line
  68.      print }'            # print the rest of the lines
  69. ------
  70. ===================================================================
  71.     Ken Kress, Glenside, PA * dsinc!phent!kkress 
  72. The moment one commits oneself then Providence moves too.
  73.             W.H. Murray, "Scottish Himalayan Expedition
  74. -------------------------------------------------------------------
  75.