Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 20 May 2012 18:22:45 +0200
From:      Polytropon <freebsd@edvax.de>
To:        Tim Dunphy <bluethundr@gmail.com>
Cc:        freebsd-questions <freebsd-questions@freebsd.org>
Subject:   Re: eliminate character with sed
Message-ID:  <20120520182245.b04d681f.freebsd@edvax.de>
In-Reply-To: <CAOZy0enEPQNFrJv-xZ-H5_18_r5rvjHQQCeFQT6x26TGTf0c0g@mail.gmail.com>
References:  <CAOZy0enEPQNFrJv-xZ-H5_18_r5rvjHQQCeFQT6x26TGTf0c0g@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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. :-)



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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120520182245.b04d681f.freebsd>