home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!nucsrl!ddsw1!dattier
- From: dattier@ddsw1.mcs.com (David W. Tamkin)
- Subject: Re: SED GURUS: Some Newline Questions
- Message-ID: <BuHy8M.3EA@ddsw1.mcs.com>
- Keywords: sed newline
- Organization: Contributor Account at ddsw1, Chicago, Illinois 60657
- References: <1992Sep10.230603.1267@ncsu.edu>
- Date: Sun, 13 Sep 1992 03:12:21 GMT
- Lines: 92
-
- fester@mamushka.ncsu.edu wrote in <1992Sep10.230603.1267@ncsu.edu>:
-
- | Hi sed people. For a project I'm working on, I'm trying to
- | figure out how to insert newlines in sed. Here's the scenario:
- |
- | 1) In the input text, lines ending with a semicolon need to have
- | a right bracket and newline inserted:
-
- What you show there are braces; the right bracket is "]".
-
- | --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.
-
- You're adding a space and a right brace there. (No, the right brace doesn't
- need escaping inside a text string.) Do you want a space as well? I'll
- assume not.
-
- There are at least four ways to go:
-
- # This assumes that the hold space, which starts out empty, will stay empty:
- sed '/;$/{
- G
- s/$/}/
- }'
-
- [Append a newline and the hold space and then place a right brace at the
- right anchor.]
-
- # This one must be strong-quoted or placed in a script file for sed -f:
- sed 's/;$/&\
- /'
-
- [(Well, it could work weak-quoted if you use two backslashes.) Place a
- newline and a right brace at the right anchor.]
-
- # The same cautions to protect the trailing backslash apply to this one:
- sed '/;$/a\
- }'
- [Output a line consisting of a right brace after you finish with the line
- being processed.]
-
- # If you don't need the hold space for anything else, keep a brace there:
- sed '1{
- h
- s/.*/}/
- x
- }
- /;$/G'
- [Put a right brace into the hold space and append it (with a separating
- newline) to every line otherwise ending in a semicolon.]
-
- | 2) Next, I need to delete a newline from text:
-
- You need to use N to get both lines (with a separating embedded newline) into
- the pattern space at once: it's all in The Ferkakte Manual. Then you can
- search for an embedded newline with \n (unfortunately, \n doesn't work in
- replacement strings).
-
- To change every occurrence of
-
- pattern1 to pattern1 pattern2
- pattern2
-
- you can do something like this (just trust me):
-
- sed 'N
- s/\(pattern1\)\n\(pattern2\)/\1 \2/
- $ b
- P
- D'
-
- David W. Tamkin Box 59297 Northtown Station, Illinois 60659-0297
- dattier@ddsw1.mcs.com CompuServe: 73720,1570 MCI Mail: 426-1818
-