eliminate character with sed

Polytropon freebsd at edvax.de
Sun May 20 16:22:46 UTC 2012

On Sun, 20 May 2012 12:08:04 -0400, Tim Dunphy wrote:
> Hello list,
>  I have a few php config files that have the windows delimiter
> character in them  ('^M') that I would like to get rid of. I'm trying
> to use sed to do it, and for some reason I am not having any luck.
> Here's the line that I'm trying to use:
>  #sed -i '.bak' 's/^M//g' config.php
> However when I have a look at the backup file that's been created with
> this command, it looks like there was no effect:
> <?php ^M/*   Global Variables   */^M        if(!defined('DS'))^M
>                 define('DS',DIRECTORY_SEPARATOR);^M^M
>         if(!defined("_MAINSITEPATH_"))^M
> define("_MAINSITEPATH_",dirname(__FILE__).DS);^M
> I was wondering is someone had a tip on how to run this command
> effectively in this situation.

It seems that you need proper quoting. The character
sequence "^M" != ASCII code of CR. Note that ^M is
just a _visual representation_ of Ctrl-M, which does
output the same ASCII code as the "CR key" would.
The corresponding escape sequence is \r. Together
with \n, the DOS-based "line end" \r\n is generated.
Remove the \r part, and you have the default (normal)
line end - as you correctly intended.

Note that different viewers or editors might have a
different representation than "^M"!

As you just want to delete the ^M (the CR), why not
use "tr" instead?

	% tr -d '\r' < config.php > config.php.new

Put a bit of scripting around it, and it will do the
same. See "man tr" for details.

Regarding the use of "sed": I'm not sure if it's possible
to do something like

	% sed -i '.bak' 's/\r//g' config.php

because I assume (not tested!) that it's not possible to
put in escape sequences like that. But try for yourself
and surprise me. :-)

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list