3

I have a log file which contains a bunch of non visible control characters such as hex \u0003.

I would like to replace this using something like SED, but can't get the first part of the regex to match:

/s/^E/some_string

I am creating the ^E by pressing CTRL-V CTRL-0 CTRL-3 to create the special character, as read from the 'man ascii' page:

003 3 03 ETX

However, nothing matches this control character.

Any help appreciated!

1
  • 1
    I don't know how you're getting that ^E (what Ctrl+0 Ctrl+3 do depend on your terminal emulator and keyboard layout). Character 3 would be Ctrl+V Ctrl+C. Jan 14, 2011 at 22:32

4 Answers 4

5

You can also use the tr command. For example:

To delete the control character:

tr -d '\033' < file

To replace the control character with another:

tr '\033' 'x' < file

If you are not sure what the value of the control character is, perform an octal dump and it will print it out:

$ cat file
hello
^[
world

$ od -b file    
0000000 150 145 154 154 157 012 033 012 167 157 162 154 144 012
0000016

So the value of control character ^[ is \033.

2

This perl one-liner will do the job - beware, it will modify the file:

perl -i -pe 's#\x{0003}#some_string#g' /path/to/log/file

If you want to replace a number of characters with character codes between a specified range:

echo {A..Z} | perl -i -pe 's#[\x{0040}-\x{0047}]#P#g'
P P P P P P P H I J K L M N O P Q R S T U V W X Y Z 

(echo {A..Z} produces a string of alphabetic characters in bash)

1
  • 1
    And if you don't want to modify the file, just drop the -i to use perl as a filter. Jan 14, 2011 at 22:31
2

This will replace all non-printable characters with a #

sed 's/[^[:print:]]/#/g' logfile
0

I'm not sure if I understand what you want, but if it is to substitute for occurrences of the successive hex bytes 0x00 0x03, this should work:

$ echo '0 61 20 00 03 0A' | xxd -r | sed 's/\x00\x03/test/g' 
a test

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .