Putting in newlines using sed

michael b maxwell michaelm at bcsaic.UUCP
Sat Nov 16 04:29:19 AEST 1985


This is on BSD 4.2...
I have a process putting out lines that look something like this:
	fjlkjf foo (a b c) jflkjf (jfkj)  foo (x y a) fjjfj
I want to break up the line so that each "foo" followed by its
parenthesized list of arguments appears on a separate line.  I don't
care what happens to the other "words;" they are eliminated in
later processing.

My first thought was to use sed to do the ff:
	s/foo/^Mfoo/g
-where "^M" is what I get by typing a ^V followed by a carriage return.
What sed does with this is to substitute a ^M, i.e. a carriage return--
but no line feed.  On the screen, the result is that you only see
the last "foo" plus whatever follows it; all the rest are overprinted.
And naturally this isn't what the following processes in the pipeline
expect...  So then I tried using to do the following:
	s/foo/^Jfoo/g
Well, I couldn't get the ^J in there!  I couldn't hide it from the shell
with ^V, \, ', " or anything else I could think of (I even tried using
vi to put it into a file of sed commands).  As far as I can tell, there's no 
way of writing the s/... line above with a *real* linefeed in place of the ^
and J.

Next, I tried ex.  Now ex will put a linefeed in, rather than a carriage
return, given the first s/... line above;  but as far as I can see, ex 
can't edit a pipeline-- it only edits files (which is why sed exists, I 
imagine!).  (You can get ex to send its output to a pipe, by doing %p.)

What I finally settled on was to use sed to insert ^Ms, then tr to
translate the ^Ms to ^Js (linefeeds).  Can you say "kludge"?  It works,
but at the cost of quite a bit of overhead--the next step in the
processing uses sed again, so the pipeline looks like:
	... \ 
	| sed (insert ^Ms) \
	| tr (convert ^Ms to ^Js) \
	| sed (command A; command B; command C...) \
	| ...
If I could eliminate the tr step, I could make "insert linefeed"
be the first command that sed does, thus eliminating two processes.

So far as I can say, there is no way of using tr to insert the ^Js in
the first place.  And no, we don't have rpl.  If I ever have time to sit
down and learn emacs, I'll probably find that I can use that, though I'm
not sure it can edit a stream rather than a file.  (Comments?)

Any ideas?
-- 
Mike Maxwell
Boeing Artificial Intelligence Center
	...uw-beaver!uw-june!bcsaic!michaelm



More information about the Comp.unix mailing list