hasan
hasan

Reputation: 19

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-packets: 99133

Upvotes: 1

Views: 3728

Answers (3)

kenorb
kenorb

Reputation: 166737

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

Upvotes: 0

Gergely Bacso
Gergely Bacso

Reputation: 14651

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)

Upvotes: 1

Bohemian
Bohemian

Reputation: 425208

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).

Upvotes: 2

Related Questions