1

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
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
hasan
  • 19
  • 1
  • 2
  • Do you want to remove ONLY ^[, or all of the escape sequences? – Jonathan Hall Jun 30 '11 at 10:11
  • Where does this come from ? A file ? The `^[` is a symbol for escape (ASCII character 27/0x1B), and IIRC `^[[0m` is a sequence to reset the character color and attributes. – DarkDust Jun 30 '11 at 10:12
  • See [this duplicate](http://stackoverflow.com/questions/6534556/how-do-we-remove-and-all-of-the-escape-sequences-in-a-file-using-linux-shell) question, it has better answers – Luke H Jun 03 '14 at 00:03

3 Answers3

2

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

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)

kenorb
  • 155,785
  • 88
  • 678
  • 743
Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
0

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
kenorb
  • 155,785
  • 88
  • 678
  • 743