how to add carriage returns

Greetings all

I often download lyrics to songs and often they copy into MS Word without hard carriage returns-so they look as one huge paragraph.
I think the solution is that I must first find out what escape characters are in there, then search and replace with maybe 0A, 0D hex, or something like this.
Can you tell me first, how to view the actual characters in Terminal or any other program, then how to search and replace.
can this be done in Text Edit, or Ms Word?

Thanks much.

Mac Pro 2.8 GHz 2x quad, Mac OS X (10.6.4), Final Cut Studio 2.x

Posted on Sep 28, 2010 5:13 PM

Reply
21 replies

Sep 28, 2010 6:11 PM in response to chipc

Check out the thread at

http://hints.macworld.com/article.php?story=20031018164326986

Or: from http://sed.sourceforge.net/sed1line.txt :
Using the Unix stream editor sed (type man sed in Terminal for usage):

# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
sed 's/.$//' # assumes that all lines end with CR/LF
sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M
sed 's/\x0D$//' # works on ssed, gsed 3.02.80 or higher

# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
sed "s/$/`echo -e \\\r`/" # command line under ksh
sed 's/$'"/`echo \\\r`/" # command line under bash
sed "s/$/`echo \\\r`/" # command line under zsh
sed 's/$/\r/' # gsed 3.02.80 or higher

# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
sed "s/$//" # method 1
sed -n p # method 2

# IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
# Can only be done with UnxUtils sed, version 4.0.7 or higher. The
# UnxUtils version can be identified by the custom "--text" switch
# which appears when you use the "--help" switch. Otherwise, changing
# DOS newlines to Unix newlines cannot be done with sed in a DOS
# environment. Use "tr" instead.
sed "s/\r//" infile >outfile # UnxUtils sed v4.0.7 or higher
tr -d \r <infile >outfile # GNU tr version 1.22 or higher

Sep 29, 2010 6:59 AM in response to chipc

I've you're unsure of the line endings, the following will take care of pc and legacy mac lines (it will also leave unix lines unchanged):
<pre style="border: 1px solid #ddd; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 40ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">perl -pi -e 's/\r\n?/\n/g'</pre>

Sep 29, 2010 9:40 AM in response to chipc

Thanks for all the info, I am sorting through the possibilities now.

But one thing would help:
When I used to work in DOS, we had a way of displaying the file where the actual hex codes were on the left and the ascii equivalents were on the right.
Is such a thing or similar available from terminal or shareware stand alone app. I would love to see exactly what I have without translating a bunch of escape sequences.

Sep 29, 2010 9:44 AM in response to chipc

When I used to work in DOS, we had a way of displaying the file where the actual hex codes were on the left and the ascii equivalents were on the right.
Is such a thing or similar available from terminal or shareware stand alone app.


Try hexdump:
<pre style="border: 1px solid #ddd; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 40ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">hexdump -C your-file</pre>

Sep 29, 2010 10:56 AM in response to chipc

Tony T1
Here is what I get. This is an old song by Ukulele Ike.
Brackets are added as to your request. Not sue
{M-^@®‚M-^@®(C)Pardon me, but¬†(Dm)if I may‚M-^@®(C)I would like to speak to you‚M-^@®(Dm)Maybe you have noticed too‚M-^@®(C)Our dogs have ¬†(G7)learned to care‚M-^@®(C)You can see it ¬†(Dm)in their eyes‚M-^@®}

But this is copy and paste of mine is not indicating what I see in the cat -vte result.
I see on my terminal screen ?M-^@? for line breaks:
Question mark
Capitol M
minus sign
Up arrow
At sign
Question mark

Sep 29, 2010 1:22 PM in response to chipc

Very strange. What does the 'file' command report? (i.e. file filename.txt).
?M-^@?

M-^ looks like 94 dec. Add 128 (high-bit) = 222, convert to hex = de.
sed can strip this with:
{code}
s/'$'\xde''$//
{code}

...but I'm not sure of the other characters, or why cat is reporting a '?'


If you post the url where you got the lyrics, I'll take a look.

Sep 29, 2010 1:41 PM in response to chipc

Please ask some Microsoft folks about your Microsoft Office software.

Given cut-and-paste works most everywhere else, there's likely a setting or tool or wizard here that can adjust the particular cut-and-paste behavior here.

My guess is that the approach here of reverse-engineering sequential file formats isn't the way that the Microsoft folks expected this to work.

Sep 29, 2010 3:29 PM in response to chipc

Another utility you might look at is iconv. For example the log format for Retrospect was recently switched to an MS unicode flavor. I discovered I could convert it to utf8 with the following command:
<pre style="border: 1px solid #ccc; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 60ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">iconv -f UCS-2LE -t UTF-8 operations_log.utx</pre>

Sep 30, 2010 3:55 PM in response to chipc

Well I have found a workaround, but the whole idea is interesting -- trying to see what characters are placed instead of hex 0a0d why and how to search and replace. The words I want to copy to a Ms Word file are at http://alles-uke.de/BluePages/mydogloves.htm

The workaround: Select and copy from web page to Text Edit. There I see the correct line breaks, I can modify text size etc, and it prints correctly.

It seems that every program wants to add and subtract formatting in cutting and pasting. The web source has oodles of html tags which are filtered out by Text Edit.

Do you guys recommend a shareware editor does not add any formatting –what do you use primarily – bbedit?

Thanks for all input.

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.

how to add carriage returns

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