Jay Taylor's notes
back to listing indexshell - Remove ANSI color codes from a text file using bash - Stack Overflow
[web search]-
Home
-
-
TeamsStack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Create a free Team Why Teams?
-
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
^[[38;1;32mHello^[[39m
^[[38;1;31mUser^[[39m
so I just want to be left with Hello and User
-
-
2
3 Answers
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
-
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.– JasenJul 1, 2015 at 5:22
-
-
3
My solution:
... | sed $'s/e[[0-9;:]*[a-zA-Z]//g'
The colon is there to support escapes for some old terminal types.
-
-
Ah, it's for the 'e' to be interpolated: stackoverflow.com/questions/11966312/…– fstamourMar 10, 2021 at 14:47
-
-
Tested this successfully with:
echo $'033'"[38;1;32mHello"$'033'"[39m"
(from Jasen's comment here )– ShadiJan 25 at 21:21
Does this solve the issue?
$ echo "^[[38;1;32mHello^[[39m" | sed -e 's/^[[[0-9;]{2,}m//g'
Hello
cheers!!
-
No, it doesn't, because
^[
represents the ASCII Escape character (Control-[, code 27).– ArmaliOct 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!!– MacUsersOct 30, 2013 at 15:51
-
-
-
3I 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.– JasenJul 1, 2015 at 5:17
Your Answer
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.
- The Overflow Blog
-
-
- Featured on Meta
-
-
-
-
-
Linked
Related
Hot Network Questions
-
GitHub SOURCE CODE LICENSE AGREEMENT, What is illegal?
-
Can you identify these dresses
-
What's this w-shaped ebony item from my grandma's violin case?
-
Discovering intended cable orientation with an older IDE controller
-
Can I cut out a leak, in the middle of a stainless braided water line, and splice it back, under the kitchen sink?
-
Can I trick an innocent third party into doing something that would be illegal if the third party had mens rea without either of us being guilty?
-
Approximately what percent of the sky has nothing in it?
-
Purpose of 了 in 有点儿贵了
-
Basic scoped timer struct design
-
Help me sell old donuts
-
Could a submarine 'sail' the ocean currents?
-
Analysis of Tarrega's Serenata Espanola
-
Do stainless steel sauce pots wear out and how can I tell if it needs replacement?
-
Fantasy novel about a house that controls the world
-
Is the Law eternal?
-
Are there seven consecutive annual dates which result in Monday-Sunday?
-
Identification of play about fictionalizing an in-universe murder
-
How do I look more like I'm on a call when I'm sat at my desk
-
Create a bingo card generator
-
How do we know we're not getting bigger?
-
Is it common that the sales team haggle with software vendors?
-
Is there a canonical name for a polynomial-like expression allowing for negative powers?
-
How accurate is this figure by TIME magazine?
-
I was knit a sweater