remove ANSI Escape characters from text file | Unix Linux Forums | UNIX for Advanced & Expert Users

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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.

Code:
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 12:25 AM..
Sponsored Links
    #2  
Old 08-26-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 672 Times in 642 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.


Code:
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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 10:14 PM
escape space characters in loop from file linuca Shell Programming and Scripting 5 12-04-2009 10:20 AM
Remove escape characters from string gio001 UNIX for Advanced & Expert Users 2 07-11-2009 08:10 AM
sed to remove 1st two characters every line of text file ajp7701 Shell Programming and Scripting 20 06-09-2009 04:06 PM



All times are GMT -4. The time now is 12:57 PM.