Unix Shell - removing rogue characters with sed

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > removing rogue characters with sed





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author removing rogue characters with sed
waffle.horn@googlemail.com

2007-05-27, 1:17 pm

Hi all,

Im trying to remove two types of rogue characters but I can't seem to
think of the correct solution with sed. The characters Im trying to
remove are the @ and ` characters shown below

-0.1000@-0.1000

-0.1000`-0.1000

I have tried...

sed 's/@/ /' FILENAME | sed 's/`/ /'

It doesn't seem to work for all instances. Any idea would be much
appreciated

Many thanks.

Andrew Smallshaw

2007-05-27, 1:17 pm

On 2007-05-27, waffle.horn@googlemail.com <waffle.horn@googlemail.com> wrote:
> Hi all,
>
> Im trying to remove two types of rogue characters but I can't seem to
> think of the correct solution with sed. The characters Im trying to
> remove are the @ and ` characters shown below
>
> -0.1000@-0.1000
>
> -0.1000`-0.1000
>
> I have tried...
>
> sed 's/@/ /' FILENAME | sed 's/`/ /'


You probably weant the 'g' option to s to make the replacements
global. Without it sed will only replace the first occurance.

You also don't need to call sed twice like this. You can feed sed
more than one command at a time, or in this instance combine the
commands into one:

sed 's/[@`]/ /g' FILENAME

That will replace either a "@" or "`" with spaces like your example
above did. To actually delete them, replace the command to sed
with 's/[@`]//g'

--
Andrew Smallshaw
andrews@sdf.lonestar.org
John W. Krahn

2007-05-27, 7:18 pm

waffle.horn@googlemail.com wrote:
>
> Im trying to remove two types of rogue characters but I can't seem to
> think of the correct solution with sed. The characters Im trying to
> remove are the @ and ` characters shown below
>
> -0.1000@-0.1000
>
> -0.1000`-0.1000
>
> I have tried...
>
> sed 's/@/ /' FILENAME | sed 's/`/ /'
>
> It doesn't seem to work for all instances. Any idea would be much
> appreciated


If you want to remove them:

tr -d '@`' < FILENAME

If you want to replace them with a space like your example shows:

tr '@`' ' ' < FILENAME



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
badger

2007-05-27, 7:18 pm

Thank you both examples seemed to do the trick. The help is much
appreciated as are the explanations.

On May 27, 8:06 pm, "John W. Krahn" <some...@example.com> wrote:
> waffle.h...@googlemail.com wrote:
>
>
>
>
>
>
>
> If you want to remove them:
>
> tr -d '@`' < FILENAME
>
> If you want to replace them with a space like your example shows:
>
> tr '@`' ' ' < FILENAME
>
> John
> --
> PERL isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall



waffle.horn@googlemail.com

2007-05-27, 7:18 pm

Thank you both examples seemed to do the trick. The help is much
appreciated as are the explanations.

On May 27, 8:06 pm, "John W. Krahn" <some...@example.com> wrote:
> waffle.h...@googlemail.com wrote:
>
>
>
>
>
>
>
> If you want to remove them:
>
> tr -d '@`' < FILENAME
>
> If you want to replace them with a space like your example shows:
>
> tr '@`' ' ' < FILENAME
>
> John
> --
> PERL isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall



Chris F.A. Johnson

2007-05-28, 7:19 am

On 2007-05-27, waffle.horn@googlemail.com wrote:
> Hi all,
>
> Im trying to remove two types of rogue characters but I can't seem to
> think of the correct solution with sed. The characters Im trying to
> remove are the @ and ` characters shown below
>
> -0.1000@-0.1000
>
> -0.1000`-0.1000
>
> I have tried...
>
> sed 's/@/ /' FILENAME | sed 's/`/ /'
>
> It doesn't seem to work for all instances.


Presumably, those instances are where there's more than one such
character on a line. For that you need to use global substitution
(and you can do it with a single call to sed):

sed -e 's/@/ /g' -e 's/`/ /g' FILENAME

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com