PDA

View Full Version : Control Character Problem


WhatMeWorry
2005.06.14, 04:36 PM
I've moved a whole lot of SimpleText files on OS 9.0 over to a new Tiger
system. The SimpleText files were used as input into a C++ program
via the cin ooperator.

My C++ program was aborting. Turned out that the SimpleText files where
now TextEdit files and they weren't the same formats. Found a "Make Plain Text" command in the TextEdit files. This almost works except the GDB debugger
now showed the file lines as being concated: line1\rline2\line3\rline4\r...<eof>

Went into vi comand at a Terminal and all the lines of the files were of course concatenated as well, but here it shows ^M at all occurrances of the Carriage
Return. I was able to manually delete the ^M and then insert a "return".
I saved of the files and my C++ program then worked fine.

The problem is that there are thousands of these \r (GDB) or ^M (vim) and I
don't know how to automate this process.

Can someone recommed an approach? I thought of grep, but how does one
differentiate between the control characters ^M (Carriage Return) and the
literal string "^M"

thanks in advance.

frozendevil
2005.06.14, 04:47 PM
http://www.macosxhints.com/article.php?story=20001206164827794

schway

edit: I had to do this earlier today and used this one (command line of course)
tr -d 'r' < file.txt > file.txt

and, unlike most examples, those <> need to be there

WhatMeWorry
2005.06.14, 05:10 PM
Thanks. I should have done a little more research before posting.

I did come across a site that mentioned \r (Mac) to \n (Unix) conversion
but it of course did not have a full command and man grep always
overwhelms me.

WhatMeWorry
2005.06.14, 06:30 PM
The previous link says this:

tr 'r' 'n' < file.txt > file.txt
This will replace the carriage returns (^M) with unix linefeeds.

However, this didn't work for me.

I tried this, but it too did not work:

tr \r \n < file.txt > file.txt


It finally worked with this variation:

tr "\r" "\n" < fileMac.txt > fileUnix.txt

PowerMacX
2005.06.14, 11:27 PM
Even easier, download TextWrangler (http://www.barebones.com/products/textwrangler/index.shtml), open the file(s), click on the File Options dropdown button and choose whatever line endings you need.
Alternatively, you can enter search/replace strings including \r & \n (control chars) or even reg. exp, and do a search & replace on all open files, all files in a folder, etc.
:)

Andrew
2005.06.15, 01:35 AM
Also, in Xcode, if you get info on a text file, you can change the line endings and character encoding of that file

frozendevil
2005.06.15, 09:57 AM
Even easier, download TextWrangler (http://www.barebones.com/products/textwrangler/index.shtml), open the file(s), click on the File Options dropdown button and choose whatever line endings you need.
Alternatively, you can enter search/replace strings including \r & \n (control chars) or even reg. exp, and do a search & replace on all open files, all files in a folder, etc.
:)

Yeah, I got use to changing line ending formats in BBEdit with the menu. When I had to do it yesterady (sans BBEdit) I was completely lost for a bit.

I suppose TextWrangler works the same a BBEdit(lite) :)

If I recall there's a command line program (dos2unix maybe?) that'll convert between windows and unix line breaks, if you ever need that.