Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question
    
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. –  Gilles Jan 14 '11 at 22:32

4 Answers 4

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.

share|improve this answer

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)

share|improve this answer
    
And if you don't want to modify the file, just drop the -i to use perl as a filter. –  Gilles Jan 14 '11 at 22:31

This will replace all non-printable characters with a #

sed 's/[^[:print:]]/#/g' logfile
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.