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?
Try:
sed 's/^A/foo/g' file
Use Ctrl+V+A to create the ^A
sequence in the above command.
By "replace", I'm assuming you want to change the file 'in-place'.
Using GNU sed:
# Create a file with a single control character (SOH)
echo -e "\x01" > file
# Change SOH control characters to the literal string "SOH"
sed -i 's/\x01/SOH/g' file
# Check result
cat file
gives...
SOH
The -i
option doesn't work on OS X sed, so you'd need to work-around that by piping sed to a temporary file.