Remove ANSI color codes from a text file using bash

前端 未结 3 1956
南方客
南方客 2020-12-20 12:15

I have a bash script that runs and outputs to a text file however the colour codes it uses are also included what i\'d like to know is how to remove them from the file, ie

相关标签:
3条回答
  • 2020-12-20 12:33

    My solution:

    ... | sed $'s/\e\\[[0-9;:]*[a-zA-Z]//g'
    

    The colon is there to support escapes for some old terminal types.

    0 讨论(0)
  • 2020-12-20 12:39
    sed -r "s/\x1B\[(([0-9]{1,2})?(;)?([0-9]{1,2})?)?[m,K,H,f,J]//g" file_name
    

    this command removes the special characters and color codes from the file

    these are some of ANSI codes: ESC[#;#H or ESC[#;#f moves cursor to line #, column # ESC[2J clear screen and home cursor ESC[K clear to end of line,

    note in case of clear code there is neither number nor semicolon ;

    agree with below comment: if the numbers are more than 2 digit kindly use this:

    sed -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" filename
    
    0 讨论(0)
  • 2020-12-20 12:40

    Does this solve the issue?

    $ echo "^[[38;1;32mHello^[[39m" | sed -e 's/\^\[\[[0-9;]\{2,\}m//g'
    Hello
    

    cheers!!

    0 讨论(0)
提交回复
热议问题