Unix/Linux Go Back    

UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

remove ANSI Escape characters from text file

UNIX for Advanced & Expert Users

Closed Linux or Unix Question    
Thread Tools Search this Thread Display Modes
Old Unix and Linux 08-24-2010
jwzumwalt jwzumwalt is offline
Registered User
Join Date: Aug 2007
Last Activity: 30 August 2010, 2:36 PM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
remove ANSI Escape characters from text file

I am trying to remove ansi escape characters from a text file. I did a google search and found perl and php answers but could not find a bash sed solution.

here are the sequences I need to remove. There is a hidden control character "ESC" (ASCII decimal 27, or hex 0x1B, or octal 033)
prior to each sequence. The code doesn't show it, but when you see the \\ the "ESC" char is being referanced. For
example 's/\\[1\;33m//' is really 's/\"esc"\[1\;33m//'.
# [1m
# [2m
# [1;31m
# [1;32m
# [1;33m
# [1;33m

This is some of the attempts I have made, but none of them work.

sed 's/0m//'  $HOME/jz-projects/test.log
sed 's/1m//'  $HOME/jz-projects/test.log
sed 's/\\[2m//'  $HOME/jz-projects/test.log
sed 's/*1\;31m//g' $HOME/jz-projects/test.log
sed 's/*1;32m*//g'  $HOME/jz-projects/test.log
sed 's/\\[1\;33m//' $HOME/jz-projects/test.log
sed 's/\\[1\;34m//' $HOME/jz-projects/test.log

thanks for the help

Last edited by jwzumwalt; 08-25-2010 at 01:25 AM..
Sponsored Links
Old Unix and Linux 08-26-2010
methyl methyl is offline Forum Advisor  
Join Date: Mar 2008
Last Activity: 5 December 2015, 9:07 PM EST
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
How I'd do it in ksh. Might need converting for bash because I have used "echo".

Generate the Escape character and hold it in a variable.
Then append the rest of the escape sequence between single quotes and also provided a backslash wherever a character might clash with "sed" commands.

ESC="`echo '\0033\c'`"    # Escape character
ES1="${ESC}"'\[1m'         # Escape sequence  <Esc>[1m

sed -e "s/${ES1}//g"  $HOME/jz-projects/test.log

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove special characters from text file kkb Shell Programming and Scripting 10 12-07-2009 11:14 PM
escape space characters in loop from file linuca Shell Programming and Scripting 5 12-04-2009 11:20 AM
Remove escape characters from string gio001 UNIX for Advanced & Expert Users 2 07-11-2009 09:10 AM
sed to remove 1st two characters every line of text file ajp7701 Shell Programming and Scripting 20 06-09-2009 05:06 PM

All times are GMT -4. The time now is 05:08 AM.