home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!princeton!tex.Princeton.EDU!subbarao
- From: subbarao@tex.Princeton.EDU (Kartik Subbarao)
- Subject: Re: SED GURUS: Some Newline Questions
- Message-ID: <1992Sep11.023617.9472@Princeton.EDU>
- Sender: news@Princeton.EDU (USENET News System)
- Nntp-Posting-Host: tex.princeton.edu
- Organization: Princeton University
- References: <1992Sep10.230603.1267@ncsu.edu>
- Date: Fri, 11 Sep 1992 02:36:17 GMT
- Lines: 89
-
- In article <1992Sep10.230603.1267@ncsu.edu> fester@mamushka.ncsu.edu (Cousin It) writes:
- >
- >Hi sed people. For a project I'm working on, I'm trying to
- >figure out how to insert newlines in sed. Man pages were
- >virtually useless, although I did determine that the "N"
- >might be necessary. Here's the scenario:
-
- Heh. Newlines with sed often pose problems. That's why I often say (and do)
- "Just use perl." But I'll bite. Just for the fun of it, I'll also give a perly
- answer.
-
- >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
- >
- >I've got this much:
- >
- > sed -e 's/;$/; \}/'
- > ^necessary?
- >
- >Adding \n, \\n, or \\\n was fruitless.
-
- Remember, inserting a newline is as simple as hitting return :-)
-
- For bourne-ish shells:
-
- $ sed 's/;$/; }\
- /' filename
-
- For the csh family: (Or like me, for zsh with cshjunkiequotes set)
-
- % sed 's/;$/; }\\
- /' filename
-
- The perlier version:
-
- % perl -pe 's/;$/; }\n/' filename
-
- >2) Next, I need to delete a newline from text:
- >
- >--before--
- >start
- >TITLE 1 | |
- >subtopic
- >
- >end
- >
- >--after--
- >start
- >TITLE 1 | | subtopic
- >
- >end
- >
- >Any ideas?
-
- Easy in perl:
-
- perl -pe 'chop if 'TITLE 1 \| \|/' filename
-
- As to a sed solution, well, I'll defer to some sed expert who can post a
- cleaner version than what I would have posted. Just use perl.
-
- I've tried rewriting the whole thing in awk and perl,
- >but it didn't work properly in awk, and I don't know perl well
- >enough to get the thing to work in perl, although I did try the
- >s2p program. Awk was a _lot_ easier to learn than perl,
- >at least for me...
-
- I'm not sure what the general form of 'TITLE 1 | |' is, but you could use
- regular expressions in perl to do that.
-
- Aw, come on -- give perl a try. Sit down with the Camel book for an afternoon
- and you'll soon be well on your way.
-
- -Kartik
-