How to prevent the pattern "^[[0m" from being written to a file ????

Hi folks,

I am using a shell script to display the referred libraries names of any specified cpp code. Given below is the script:

shell script "grblib"
-------------------------------------------------------------------------
#!/bin/sh
# get the lines having "include" pattern
libMatch=`strMatch $1 "include"`

# check each lines
for x in $libMatch; do
tknFlag=0
libTkn=`echo $x`

     \# Flag to check .h extension \(add new to filter them in\)
     hExtTknFlag=\`echo $x | grep -c "\\.h"\`

     \# Common filtering based on pattern
     rTknFlag=\`echo $x | grep -c ">"\`
     dTknFlag=\`echo $x | grep -c "\\$include\\""\`
     hshTknFlag=\`echo $x | grep -c "\#include <"\`

     if [ $dTknFlag -eq 1 ]; then
             tknFlag=1
             libTkn=\`echo $libTkn | sed 's/$//g'\`
             libTkn=\`echo $libTkn | sed 's/"//g'\`
             libTkn=\`echo $libTkn | tr -d ";"\`
     else

             if [ $hshTknFlag -eq 1 ] || [ $rTknFlag -eq 1 ] ; then
                     tknFlag=1
                     libTkn=\`echo $libTkn | sed 's/\#//g'\`
                     libTkn=\`echo $libTkn | sed 's/<//g'\`
                     libTkn=\`echo $libTkn | sed 's/>//g'\`
                     libTkn=\`echo $libTkn | sed 's/iolib\\///g'\`
             else
                     hshTknFlag=\`echo $x | grep -c "\#include \\""\`
                     if [ $hshTknFlag -eq 1 ]; then
                            libTkn=\`echo $libTkn | sed 's/\#//g'\`
                            libTkn=\`echo $libTkn | sed 's/"//g'\`
                            libTkn=\`echo $libTkn | tr -d ";"\`
                    else
                            tknFlag=0
                    fi
            fi
    fi

    if [ $hExtTknFlag -eq 1 ]; then

            if [ $tknFlag -eq 1 ]; then
                    \# print the final output
                    echo $libTkn  
            fi
    fi

done
-------------------------------------------------------------------------

The strMatch out puts the following from the sample.cpp file

8: // 08-06-01 JS Replaced include/dri and app with SCCS get sequence
18: #include <string.h> // String manipulation functions
19: #include <iolib/Misc.h> // Miscellaneous functions
20: $include "DriPrm.h"; // Infrastructure parameters
21: $include "DriDef.h"; // Infrastructure definitions
22: $include "GblDef.h"; // DRS global definitions
23: #include "ErrMsg.h"
24: $include "ChipImg.h"; // Chip image declarations
25: #include "MtqHdr.h" // MTQ record header
26: #include "MtqCsm.h"
27: #include "VerDef.h" // VER support functions anddefinitions

------------------------------------------------------------------------

While running the script
$grblib sample.cpp

The output comes normal and correct

string.h
Misc.h
DriPrm.h
DriDef.h
GblDef.h
ErrMsg.h
ChipImg.h
MtqHdr.h
MtqCsm.h
VerDef.h
-------------------------------------------------------------------------

But in the shell the text color after ErrMsg.h is dark grey in color, and when it is redirected to a file say tmpfile, i saw the following...

string.h
Misc.h
DriPrm.h
DriDef.h
GblDef.h
ErrMsg.h^[[0m
ChipImg.h
MtqHdr.h
MtqCsm.h^[[0m
VerDef.h

-------------------------------------------------------------------------

I have no idea from where ^[[0m is coming from....if i am right it may be due to the space after ErrMsg.h (absence of usual comment) that generates this character. Since this script is used by another one that gets the file from the library the "ErrMsg.h^[[0m" is creating trouble since it is supposed to be "ErrMsg.h"

This is the line that writes the list generated from grblib to a file
echo -e `grblib $x | tr " " "\n"` > $libFile"Lib.bas"

Since the output is not displaying ^[[0m but changing the text color alone, i need to get rid of this pattern from the name.

Kindly help.....:confused:

That is an escape sequence = escape[0m escape = ^[

Escape sequences do funny things to terminal output, like change the color for example.
You probably have different escape sequences being sent to the terminal. Trap the ouput in od:

grblib somefile.cpp | od -c

This will let you see exactly what is being output
ansi escape sequences:

ASCII Table - ANSI Escape sequences

Finally once you know which bits of garbage to filter, you can use sed to filter them out.

grblib somefile.cpp | sed 's'/<garbage>//g'

I do not know what junk is being produced.

Note: You may also want to check your terminal settings.

Hi,

Thnks for the reply.
I have used the following command as you told...
grblib VerCsm.cpp | od -c

the output is

0000000 s t r i n g . h \n M i s c . h \n
0000020 D r i P r m . h \n D r i D e f .
0000040 h \n G b l D e f . h \n E r r M s
0000060 g . h 033 [ 0 m \n C h i p I m g .
0000100 h \n M t q H d r . h \n M t q C s
0000120 m . h 033 [ 0 m \n V e r D e f . h
0000140 \n
0000141
-------------------------------------------------------------------------
Then i have added some sed and tr command to filter the junk out
grblib VerCsm.cpp | od -c |sed 's/033//g;s/\[//g;s/m//g;s/0//g' | tr -d "123456"

s t r i n g . h \n M i s c . h \n
D r i P r . h \n D r i D e f .
h \n G b l D e f . h \n E r r M s
g . h \n C h i p I g .
h \n M t q H d r . h \n M t q C s
. h \n V e r D e f . h
\n
-------------------------------------------------------------------------
Which is (without od)
grblib VerCsm.cpp | sed 's/033//g;s/\[//g;s/m//g;s/0//g' | tr -d "123456"

string.h
Misc.h
DriPr.h
DriDef.h
GblDef.h
ErrMsg.h
ChipIg.h
MtqHdr.h
MtqCs.h
-------------------------------------------------------------------------
when redirected to a file say tmp i saw...
string.h
Misc.h
DriPr.h
DriDef.h
GblDef.h
ErrMsg.h^[
ChipIg.h
MtqHdr.h
MtqCs.h^[
VerDef.h

-------------------------------------------------------------------------
Due to sed the 'm's are gone from ChipImg.h.etc..which was unexpected...and the redirected output tmp still showed "^[" character, which is not displayed in od output.

My terminal prompt value PS1 is

export PS1="\[\e[1;31m\][\[\e[36;1m\]\u\[\e[31;1m\]@\[\e[32;1m\]\H\[\e[1;36m\]{tty:\l}\[\e[31;1m\]]\[\e[1;37m\]\W\[\e[1;31m\]$ \[\e[1;37m\]"

which will display like this
[bas@casinox{tty:18}]bin$
-------------------------------------------------------------------------

The actual culprit was strMatch script which was displaying the output in yellow color...the script works fine when the color is removed....i use the following script to add color while echoing

#scriptfile cecho

open_escape="^[[0m"
close_escape="^[[0m"

case $1 in
red) open_escape="^[[1;31m"
shift ;;
green) open_escape="^[[1;32m"
shift ;;
blue) open_escape="^[[0;34m"
shift ;;
purple) open_escape="^[[1;35m"
shift ;;
cyan) open_escape="^[[0:36m"
shift ;;
grey) open_escape="^[[0;37m"
shift ;;
white) open_escape="^[[1;37m"
shift ;;
yellow) open_escape="^[[1;33m"
shift ;;
bold) open_escape="^[[1m"
shift ;;
*) shift ;;
esac

echo "${open_escape}$*${close_escape}"

exit 0
-------------------------------------------------------------------------
Eventhough the issue was solved by removing the color......i would like to know how to remove those junk characters..........since the solution was just an escape method but not an actual dealing with the situation.....:).

Re: remove the junk characters.

Like Jim suggested earlier, at some point in your processing pipe the data through sed and use regex for the pattern matching of what to filter out.

If you can pipe the data to other programs, then sed is probably the best (and safest) way to filter out things you don't want.

See this example to get a much better idea of what is your output

> echo "Hello stranger"                    
Hello stranger
> echo "Hello stranger" | od -An -t dC -w10
   72  101  108  108  111   32  115  116  114   97
  110  103  101  114   10
>

The numbers are the ASCII codes for the text; note the last 10 which is a 'line feed' character.

The 033 in the od output you posted is the escape character. The sequence ESC [ 0 m returns the terminal to regular black on white (or whatever default you have).