Operating System - HP-UX
1822011 Members
4102 Online
109639 Solutions
New Discussion юеВ

Re: using sed to remove special characters

 
SOLVED
Go to solution
Omololu Shobayo
Frequent Advisor

using sed to remove special characters

Please, how can I remove special characters from an xtml file. The hex value of the special character is known.
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: using sed to remove special characters

you don't need to know the hex.

special characters require a \@ in the sed statement to make a change.

sed s/steve\@aol.com/steveprotter\@aol.com/g

# not fully tested...

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Bill Hassell
Honored Contributor

Re: using sed to remove special characters

Probably easier just using tr:

tr -d "\033" myfile.xml > newfile.xml

which strips out the escape character from myfile.xml.


Bill Hassell, sysadmin
Omololu Shobayo
Frequent Advisor

Re: using sed to remove special characters

I dont want to strip out but to replace it with another special character.
H.Merijn Brand (procura
Honored Contributor

Re: using sed to remove special characters

# man tr

# tr -d x
where x is/are the special character(s)

# tr -d '\043'
will delete #'s from the file (043 being the octal representation of the ascii value of '#')

# tr -d '\043\243'
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: using sed to remove special characters

# perl -pi -e's/\234/\432/g' file

will in-place change \234 to \432 everywhere

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: using sed to remove special characters

I would simply use tr.

e.g.

cat oldfile | tr -d '\015\033' > newfile
would delete all CR (octal 15) and ESC (octal 33) characters from oldfile.
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: using sed to remove special characters

Hi,

To replace the special characters, you can use the same 'tr' command.

$cat file
Hmm... !This is a warning!
$tr -s '\042' '\041'< file
Hmm... "This is a warning"

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Omololu Shobayo
Frequent Advisor

Re: using sed to remove special characters

Thansk everyone. The actual command used in the script was as below.

cat filename|tr -s '\226' '\230\226' >newfile


the command tr -d "\256" filename was giving all sorts of error but I believe piping would solve the error issues.
H.Merijn Brand (procura
Honored Contributor

Re: using sed to remove special characters

1. In my answer I did add a redirection

# tr -d "\256" < filename

which should take care of the errors.

2. I *think* that you do not understand tr's -s option. It is *not* about substitution, but about squeezing multiple occurrances of the same character to 1

3. You should asign points to the answers

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Omololu Shobayo
Frequent Advisor

Re: using sed to remove special characters

Fortunately in this case I have only one item to subtitute.