How to remove CTRL-A characters from file using SED?
17,369
Solution 1
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
Solution 2
^A is byte 1 or \x01
so you should be able to do this:
sed 's/\x01//g'
Keep in mind that for single-byte changes, "tr" is faster than sed, although you'll have to use bash's $'..'
syntax to give it a 0x01 byte:
tr -d $'\x01'
Author by
DJElbow
Updated on July 29, 2022Comments
-
DJElbow over 1 year
I want to remove all "^A" control characters from a file using SED. I can remove all control characters using 'sed s/[[:cntrl:]]//g' but how can I specify "^A" specifically?
-
Tobia about 11 yearsI would recommend against doing this, because you won't be able to easily copy and paste it, put it into a script, edit the script with any editor, and so on.
-
Jim Stewart about 11 yearsYou can type a literal into a quoted string in Bash by prefixing it with C-v:
tr -d 'C-vC-a'
, where you literally press control-v and then control-a. -
Eric about 11 years@Tobia You can do ctrl-v ctrl-a in vi and save the file with the correct ^A in there. Copy and pasting with the GUI paste buffer is the only thing you can't do.
-
Tobia about 11 yearsTry using the
$''
in sed too:sed $'s/\x01//g'
-
DJElbow about 11 yearsYou are right. This does work with sed. For some reason it does not work on my mac, by it works on ubuntu.
-
Wildcard over 7 years@DJElbow, Mac has BSD Sed; Ubuntu has GNU Sed. They have different features. GNU Sed generally has more features. If you want Sed commands (or scripts) to be portable, it's best to stick with POSIX specified features.
-
Tobia about 6 years@Wildcard one difference that always gets me is how to edit a file in place. In GNU sed you can just add a
-i
argument:sed -i -e '...' $file
while in macOs you need an additional, separate empty argument:sed -i '' -e '...' $file
I haven't yet been able to find a single invocation that will work in both. -
Wildcard about 6 years@Tobia to portably edit a file in place, don’t use Sed at all; use “ex,” which is specified by POSIX. I’ve written many answers using “ex” if you need examples.