sed tab & new line

When using sed, what characters would I use when trying to replace new line/carriage return with tabs?

G5/dual 2ghz/2.5gb RAM, Mac OS X (10.4.5)

Posted on Mar 4, 2006 8:51 AM

Reply
2 replies

Mar 4, 2006 1:07 PM in response to David McMillan

Hi David,
With Apple's version of sed, you simply use the literal characters for carriage return and tab. Of course the shell reacts to them when they are typed so you must escape them to get them into an actual command. You can type a carriage return with the character sequence <Control>-v<Control>-m and the shell represents it with the sequence ^M. You can get a tab with the sequence <Control>-v<Tab>.

Dealing with a newline is more complex because sed is a line based utility so the newline isn't treated as part of the line. If you use sed's 'N' command, that will read the next line and append it to the pattern space with a newline between the two "lines". Thus, the command that will do what you want to the first and second lines would look like the following in the command line:

sed -e 'N;s/^M\n/<Tab>/g' <file>

Of course the <Tab> would just look like a long space and both special characters are typed as I described above. Sed does replace the "\n" with a newline.

As I said, this only works for a pair of lines. To do this with a whole document you would have to write a loop. Unfortunately I don't have time to go into that now as I must go out of town shortly. If you don't know how to do it and no one has suggested anything by tomorrow evening, I'll describe it.
--
Gary
~~~~
The best thing about growing older is that it takes such a
long time.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

sed tab & new line

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.