开发者

How to Remove ^[ characters from a file using Linux shell scripting?

Using Linux shell scripting, how can I remove the ^[ characters from something like this:

^[[0mCAM1> 
^[[0^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H
 rcv-multicast: 0
      tx-bytes: 33649974
    tx-pac开发者_如何学Ckets: 99133


You can use sed to remove chars from files like this:

sed -i '' -e 's/^[//g' somefile

The -i '' causes it to change the file in-place (not make a copy).


You can make that with sed for example:

sed 's/^\[//g' oldfile > newfile;
mv newfile oldfile;

(it will remove only the trailing brackets, if you want to remove all of them, remove the ^ sign from the sed expression)


You may remove these control characters by:

tr -d "[:cntrl:]" file.txt

however it'll remove also new line endings, so here is a trick, define this alias:

alias clean='tr "\r\n" "\275\276" | tr -d "[:cntrl:]" | tr "\275\276" "\r\n"'

then try like:

cat file.txt | clean > new_file.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜