home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sun-barr!ames!lll-winken!iggy.GW.Vitalink.COM!cs.widener.edu!dsinc!phent!phent!kkress
- From: kkress@phent.UUCP (Kenneth R. Kress)
- Newsgroups: comp.unix.questions
- Subject: sed and linefeeds
- Message-ID: <3178255@phent.UUCP>
- Date: 12 Sep 92 05:53:28 GMT
- Sender: news@phent.UUCP (GNEWS Version 2.0 news poster.)
- Organization: That's Entertainment BBS -- Glenside, Pa.
- Lines: 70
-
- fester@mamushka.ncsu.edu (Cousin It) writes:
- > Date: Thu, 10 Sep 1992 23:06:03 GMT
- > For a project I'm working on, I'm trying to
- > figure out how to insert newlines in sed. ...
- > although I did determine that the "N"
- > might be necessary. Here's the scenario:
- > 1) In the input text, lines ending with a semicolon need to have
- > a right bracket and newline inserted:
- > --before--
- > start
- > { THIS IS LINE 1;
- > { THIS IS LINE 2;
- > end
- > --after--
- > start
- >
- > { THIS IS LINE 1; }
- >
- > { THIS IS LINE 2; }
- >
- > end
- > 2) Next, I need to delete a newline from text:
- > --before--
- > start
- > TITLE 1 | |
- > subtopic
- >
- > end
- >
- > --after--
- > start
- > TITLE 1 | | subtopic
- >
- > end
- > Any ideas?
-
- I don't pretend to be an expert, but I came up with a shell
- script using sed and tr that does what you say you want.
- It requires you be able to pick a symbol that is not
- anywhere in your document. I used the at sign in my script.
-
- The script:
-
- sed 's/\;$/\; \}\@/
- /\| \|/N
- s/\n/ /' |
- tr "@" "\n"
-
- tr turns the at signs into new lines (I couldn't get
- sed to do it either, although I'm pretty sure the MKS sed
- I have at work will insert newlines.)
- As you guessed, the N is needed to put both lines into
- the work area and the substitute command replaces the newline
- with a space.
- An awk script to do the same chores:
-
- awk '{
- if ($0 ~ /;$/) { # if the line ends with semi-colon
- print $0" \}\n"; # append bracket and linefeed
- next } # fetch next input line
- if ($0 ~ /\| \|/) { # if line contains | symbols
- printf ("%s ",$0); # print without a linefeed
- next } # fetch next input line
- print }' # print the rest of the lines
- ------
- ===================================================================
- Ken Kress, Glenside, PA * dsinc!phent!kkress
- The moment one commits oneself then Providence moves too.
- W.H. Murray, "Scottish Himalayan Expedition
- -------------------------------------------------------------------
-