Jay Taylor's notes

back to listing index

shell - Remove ANSI color codes from a text file using bash - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: howto command-line ascii terminal-color-codes-stripper ansi sed stackoverflow.com
Clipped on: 2023-02-03

  1. Home
    1. Public
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Collectives
    7. Explore Collectives
    1. Teams
      Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
Asked 9 years, 3 months ago
Viewed 13k times

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


so I just want to be left with Hello and User

16.4k1818 gold badges7474 silver badges109109 bronze badges
asked Oct 10, 2013 at 13:09
75933 gold badges1616 silver badges2929 bronze badges

3 Answers

Sorted by:
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
11.6k22 gold badges2929 silver badges4747 bronze badges
answered Jun 19, 2015 at 12:54
21333 silver badges1111 bronze badges
  • there is no guarantee on the number of number parts in the code, those for setting colour can have upto 5 numbers without redundancy. the codes for setting other features (eg linux consile bell frequency) can have more than 2 digits in a row.
    – Jasen
    Jul 1, 2015 at 5:22
  • agree with you Jasen :-)
    – sandy_1111
    Jul 1, 2015 at 11:17
  • 3
    The commas in [m,K,H,f,J] are superfluous, right?
    – Tuetschek
    May 2, 2018 at 13:22

My solution:

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

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

answered Feb 6, 2019 at 6:10
2,77711 gold badge1515 silver badges2323 bronze badges

Does this solve the issue?

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


answered Oct 25, 2013 at 19:14
2,07733 gold badges3535 silver badges5252 bronze badges
  • No, it doesn't, because ^[ represents the ASCII Escape character (Control-[, code 27).
    – Armali
    Oct 30, 2013 at 9:35
  • @Armali: don't understand your point. According to the OP's requirement, it should work. Can you post an example where it doesn't work? Cheers!!
    – MacUsers
    Oct 30, 2013 at 15:51
  • Yes... TERM=xterm tput setf 1|sed -e 's/^[[[0-9;]{2,}m//g'|hexdump -C
    – Armali
    Oct 31, 2013 at 7:36
  • I don't think that what the OP is after.
    – MacUsers
    Oct 31, 2013 at 11:29
  • 3
    I think you mean echo $'033'"[38;1;32mHello"$'033'"[39m" | sed -e 's/^[[[0-9;]{2,}m//g' (what you wrote can't be pasted successfully) ,and no it doesn't work.
    – Jasen
    Jul 1, 2015 at 5:17

Your Answer

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password

Post as a guest


Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

Hot Network Questions