myahya myahya - 5 months ago 53
Bash Question

Replacing Control Character in sed

I need to replace all occurrences of the control character CTRL+A (SOH/ascii 1) in a text file in linux, how can this be achieved in SED?


This can be done through cat with the -v (equivalently --show-nonprinting options and piping this into sed).

If the control character the start of heading (SOH) character (CTRL+A / ASCII 1), and we want to replace it with a tab, we would do the following:

cat -v file | sed 's/\^A/\t/g' > out

cat -v would replace the SOH character with ^A, which would then be matched and replaced in sed.