Escaping carriage returns

simX

Unofficial Mac Genius
How do I escape carriage returns in UNIX? I don't remember how to do this.... :(
 
The backslash is the escape character for printable character. But the carriage return is not a printable character.

If you go into the Terminal, and you type \ and then a return, it still accepts the return as a real return and tries to execute the command.

Somehow, you can get the Terminal to print ^M for a carriage return, but it doesn't interpret it as an immediate return and does not execute the command.

So I just need the escape key-combo for non-printable characters.
 
I think you're wrong.

If I enter anything on the command line
and then a \, the shell goes into line
continuation mode.

It doesn't actually try to execute the
the command until you give it a line w/ a
real carriage return.
 
Haven't had to mess with DOS/Windows created files in the UNIX world much have you? :)

I can't count how many scripts I've had to filter out ^M which sit at the end of every damn line.

Brian
 
there ya go.

I've spent 99+% of my computing life w/o
messing w/ the hellhole of DOS

Of course, on Solaris, there is 'dos2unix'
and 'unix2dos' which does that for you ;)
 
perl -i -pe "s/^V^M//g" /path/to/file

Also if in say, vi you want to remove all the ^M's in command mode you'd do
:%s/^V^M//g

And bingo, all the ^M's are gone.
Same thing works for just about any non-printable character. Lots of things don't recognize octal characters, so you have to use the controled character. It comes in especially handly when dealing with files that someone stupidly put control characters into the name.

Brian
 
Use this script to strip annoying ^M characters from all files in a directory recursively. WARNING - make sure you don't run this on binary files (like images or compiled programs). Currently this script ignores directories called "CVS" and "images."

COOL ADDITIONS YOU COULD ADD (and post back):
* pass file names in on command line
* specify -r to recurse through subdirectories

======================================
#!/bin/sh
# stripMs.sh
# This script must be in your PATH to call itself recursively.
# Run in directory with tainted text files.
# Recursively searches all directories and their files.

for f in `ls`
do
if [ -d $f ]
then
if [ "$f" = "CVS" ] || [ "$f" = "images" ]
then
echo "skipping dir $f"
else
echo "going into dir $f"
cd $f
$0
cd ..
fi
else
cp $f $f.old
tr -d '\r' < $f.old > $f
echo "fixed $f"
rm $f.old
fi
done
exit 0
======================================
 
Back
Top