Tue 16 Aug 2005
Often I would edit a file on my local machine using applications such as Dreamweaver or Excel. When I read these files in a UNIX terminal, the newlines are not displayed and instead are replaced with ^M characters. This makes the file appear convoluted and virtually unreadable. Here’s a fix to convert those ^M characters back to the newline characters so that the files display correctly.
Open the file in vi and type the command
:s/\r/\r/g
This performs a global search and replace through the file, replacing \r characters with \r. I don’t know why this works, but it does!
Note: You can change your settings in Dreamweaver so that newline chars are saved for UNIX format. Go to Preferences > Code Format and select Line Break Type to LF (Unix). This eliminates the problem at the source for Dreamweaver files. I have not yet discovered a solution for Excel files, so the vi workaround works well for those.
7 Responses to “Newlines in Unix Displaying as ^M”
Leave a Reply
You must be logged in to post a comment.


August 16th, 2005 at 12:51 pm
Also, on Sun (Solaris) and Linux (Redhat) machines you can use the ‘dos2unix’ command to get rid of the extra newlines:
Suns:
dos2unix oldfile newfile
Linux:
dos2unix -n oldfile newfile
Being a good Unix command, you can also pipe STDIN into this command and output the modified file to SDTOUT on the command line. You can find out more about the options each platform offers with this command by reading the man pages: man dos2unix
There is also the analoge command, unix2dos, to make a text file palatable to a Windows machine when transfered from a Unix machine.
Using FTP ASCII mode to copy a text file to and from a Unix machine will automagically take care of the EOL problem in both directions.
August 16th, 2005 at 1:12 pm
Thanks for the tips Jerry!
Btw, for some reason your comment needed approval before it could be posted. Needless to say I approved it, but now we need to figure out why it wasn’t posted immediately.
August 16th, 2005 at 1:13 pm
My last comment was immediately posted. Perhaps because it was my post?…
August 16th, 2005 at 1:17 pm
Possibly the user level is at fault. When I set up my WP account, I gave myself the same user level (5) as you. Strange.
August 16th, 2005 at 1:22 pm
I think I found the source. I unchecked the box in the Discussion Options section that says “Comment author must have a previously approved comment.” This explains why your 2nd comment just went through, but your 1st comment needed moderation. Let’s hope so anyway.
August 18th, 2005 at 11:09 pm
A deM script approach (%deM file) used at icess. Worth establishing in a shared bin directory on iocean? -karen
#!/bin/sh
# script to remove the control-M appearing in files
# this is produced by a control-R before the control-M
if [ “$#” -lt 1 ]
then
echo “solve the problem of ^M’s in files when vi’ing.
actually a function of octal 15’s in file. This deletes them.
usage: $0 file file file”
fi
for file
do
check=`file $file | tr -s ‘ ‘ ‘ ‘ | cut -f2`
if [ “$check” = “demand” -o “$check” = “data” -o\
“$check” = “directory” -o “$check” = “executable” -o\
“$check” = “archive” ]
then
echo “$file is binary, not changed”
continue
fi
if [ ! -s “$file” ]
then
echo “file $file does not exist”
continue
fi
tr -d ‘\15′ /tmp/,tmp.$$
if [ -w “$file” ]
then
mv /tmp/,tmp.$$ $file
else
echo “$file not writable. Will write to $HOME/${file}.deM”
mv /tmp/,tmp.$$ $HOME/${file}.deM
fi
done
August 19th, 2005 at 10:11 am
Sure. Why not post that script somewhere on iocean, and we can set up a folder for shared scripts later.