Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

unix

sed

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?

like image 209
DJElbow Avatar asked Mar 14 '13 22:03

DJElbow


People also ask

How do I remove a character from a file in Linux?

Using the truncate Command. The command truncate contracts or expands a file to a given size. The truncate command with option -s -1 reduces the size of the file by one by removing the last character s from the end of the file.

How do you remove something from sed?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.


2 Answers

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

like image 144
Eric Avatar answered Sep 24 '22 03:09

Eric


^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'
like image 36
Tobia Avatar answered Sep 25 '22 03:09

Tobia