如何使用SED从文件中删除CTRL-A字符?

时间:2022-06-02 19:23:00

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?

我想删除所有“^”控制字符使用SED从一个文件中。我可以删除所有控制字符使用sed年代/[[:cntrl:]]/ / g '但我如何指定“^”特别吗?

2 个解决方案

#1


9  

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

复制“^”简单地按下ctrl - v ctrl - A这将重现^一个文件中

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

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

在这条线的^我按下ctrl - v ctrl - A的结果

#2


9  

^A is byte 1 or \x01 so you should be able to do this:

^ 1字节或\ x01所以你应该可以这样做:

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”比sed更快,尽管您必须使用bash的$“。语法为0x01字节:

tr -d $'\x01'

#1


9  

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

复制“^”简单地按下ctrl - v ctrl - A这将重现^一个文件中

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

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

在这条线的^我按下ctrl - v ctrl - A的结果

#2


9  

^A is byte 1 or \x01 so you should be able to do this:

^ 1字节或\ x01所以你应该可以这样做:

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”比sed更快,尽管您必须使用bash的$“。语法为0x01字节:

tr -d $'\x01'