8

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?

DJElbow
  • 3,345
  • 11
  • 41
  • 52

2 Answers2

11

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

Eric
  • 2,056
  • 13
  • 11
  • 1
    I 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. – Tobia Mar 14 '13 at 22:39
  • @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. – Eric Mar 14 '13 at 22:41
11

^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'
Tobia
  • 17,856
  • 6
  • 74
  • 93
  • You 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. – Jim Stewart Mar 14 '13 at 22:40
  • 1
    Try using the `$''` in sed too: `sed $'s/\x01//g'` – Tobia Mar 14 '13 at 23:54
  • You are right. This does work with sed. For some reason it does not work on my mac, by it works on ubuntu. – DJElbow Mar 16 '13 at 18:17
  • @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](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html). – Wildcard Nov 05 '16 at 03:00
  • @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. – Tobia Jan 22 '18 at 17:24
  • 1
    @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. – Wildcard Jan 22 '18 at 21:09