mobileguy
mobileguy

Reputation: 1

using sed to remove special chars and add spaces instead

I have a block of text i'd like to change up:

^@^A^@jfits^@^A^@pin^@^A^@sadface^@^A^@secret^@^A^@test^@^A^@tools^@^A^@ttttfft^@^A^@tty^@^A^@vuln^@^A^@yes^@^

using sed i'd like to remove all the ^@^A^ (and variations of those chars) with a few spaces.

I tried:

cat -A file | sed 's/\^A\^\@/  /'

but thats obviously wrong, can someone help?

Upvotes: 0

Views: 178

Answers (3)

BMW
BMW

Reputation: 45293

If only replace ^A and ^@, you can use this:

sed 's/[\x01\x0]/ /g' file

Then I find more similar answers in SO which already discussed.

https://superuser.com/questions/75130/how-to-remove-this-symbol-with-vim

Replacing Control Character in sed

Upvotes: 1

John Meacham
John Meacham

Reputation: 821

if you can enumerate the allowed characters then you can do something like

sed -e 's/[^a-zA-Z0-9]/ /g' 

which will replace everything not in the set of alphanumeric characters with a space.

If you just want to replace all 'non-printable' characters with spaces then you can use a character class[1] with

sed -e 's/[^[:print:]]/ /g'

some older versions of sed may not support this syntax though but it is standardized in the unix specification so you should not feel guilty for using it.[2]

[1] http://sed.sourceforge.net/sedfaq3.html

[2] http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_03

Upvotes: 3

alex
alex

Reputation: 12265

It looks like ^A is not two characters, but in fact just one control character. So you should write something like \x01 instead.

Anyway, there are three character ranges, \x00-\x1f are control characters, \x20-\x7f are ascii, and others are... something that depends on encoding.

I don't know sed well, but if you want ascii only, that's how I would've done it in perl:

head /dev/urandom | perl -pe 's/[^\x20-\x7f]/ /gi'

Upvotes: 2

Related Questions