Replacing Control Character in Sed

Replacing Control Character 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.

How to remove CTRL-A characters from file using SED?

to reproduce "^A" simply press Ctrl-v Ctrl-a this will reproduce the ^A in the file

sed -i -e 's/^A/BLAH/g' testfile

the ^A in that line is the result of me pressing Ctrl-v Ctrl-a

Replace control characters with sed

There are no control characters. Just echo -e to display this string:

s='Rating..........Red\nAlert.Name......High Update Response Time\nManaged.Object..D0P~ABAP~sap00d0p_D0P_48'

echo -e "$s"
Rating..........Red
Alert.Name......High Update Response Time
Managed.Object..D0P~ABAP~sap00d0p_D0P_48

sed remove a special control character from many files

POSIX sed doesn't handle NUL in input but GNU sed can with hex escape:

find . -name '*.html' -type f -exec sed -i 's/\x0//g' '{}' +

Replace text with special characters using sed

It's generally safer, and therefore often better, to use a JSON-aware tool rather than sed when editing JSON. Using jq, one possibility would be:

jq --arg name "$name" --arg newvalue "$newvalue" '
.Changes[0].ResourceRecordSet |=
(.NAME=$name
| .ResourceRecords[0].Value = $newvalue)' <<< "$rssample"

A free-form approach

jq --arg name "$name" --arg newvalue "$newvalue" '
walk(if type == "object"
then if has("NAME") then .NAME=$name else . end
| if has("Value") then .Value = $newvalue else . end
else . end)' <<< "$rssample"

sed to replace non-printable character with printable character

Sed should recognise special characters:

sed -e 's/\x0b/ /g'


Related Topics



Leave a reply



Submit