udayadevan
udayadevan

Reputation: 23

How to Replace special Character in Unix Command

My source data contains special characters not in readable format. Can anyone help on the below :

Source data:

ascii values

Commands Tryed: sed 's/../t/g' test.txt > test2.txt

Upvotes: 1

Views: 2407

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

you can use tr to keep only printable characters:

tr -cd "[:print:]" <test.txt > test2.txt

Uses tr delete option on non-printable (print criteria negated by -c option)

If you want to replace those special chars by something else (ex: X):

tr -c "[:print:]" "X" <test.txt > test2.txt

With sed, you could try that to replace non-printable by X:

sed -r 's/[^[:print:]]/X/g' text.txt > test2.txt

it works on some but fails on chars >127 (maybe because the one I tried is printable as ▒ !) on my machine whereas tr works perfectly.

inline examples (printf to generate special chars + filter + od to show bytes):

$ printf "\x01ABC\x05\xff\xe0" | od -c
0000000 001   A   B   C 005 377 340
0000007

$ printf "\x01ABC\x05\xff\xe0" | sed "s/[^[:print:]]//g" | od -c
0000000   A   B   C 377 340
0000005

$ printf "\x01ABC\x05\xff\xe0" | tr -cd "[:print:]" | od -c
0000000   A   B   C
0000003

Upvotes: 1

Related Questions