0

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?

Jotne
  • 40,548
  • 12
  • 51
  • 55

3 Answers3

3

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

John Meacham
  • 785
  • 7
  • 9
2

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'

alex
  • 11,935
  • 3
  • 30
  • 42
1

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

Community
  • 1
  • 1
BMW
  • 42,880
  • 12
  • 99
  • 116