Disabling Backslash Interpretation with "echo -E"?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Disabling Backslash Interpretation with "echo -E"?
# 8  
Old 08-16-2012
Hey Guys,

EDIT:
READ THIS FIRST:
Not sure what the heck is going on but now the same thing is happening on all the machines I'm trying this on...
So now it is changing the Escape Sequences when viewing inside the text file....
SO YOU CAN IGNORE MY QUESTION BELOW... NOT SURE WHAT HAPPENED..????


I had another question if anyone knows why this is happening?

On the one server I'm testing this on, when I print stuff out to stdout that contains Bolded Text and also
redirect the execution of the script to a file it seems to change the escape sequences like this:
From this --> \033[1m for Bold to this --> ^[[1m...

The Server that, that was run on is:
OS: SLES 11.1
Shell: /bin/bash Version 3.2.51


But then testing this on another server which is:
OS: AIX 6
Shell: /usr/bin/ksh
*But I have Bash Version 3.2.16 installed on this server...


So I guess my question is, is there something that changed between those 2 version of Bash that would
cause those Escape Sequences to be replaced by different sequences?

I can include an example if I'm not making sense...


UPDATE:
I also just went and tested the exact same code on another server which is:
OS: OpenSUSE 11.4
Shell: Bash Version 4.1.10
And after testing this on this machine as well, it does the same thing of changing the Bold
Escape Sequences from "\033[1m" --to--> "^[[1m"


Thanks in Advance,
Matt

---------- Post updated at 04:23 PM ---------- Previous update was at 02:40 PM ----------

Hey Corona, sorry didn't see your reply.

I forget exactly why I had it being read into an array first... I think it was because like every other way I tried reading the file
it would not preserve whitespace (i.e. empty lines that needed to be there).

Thanks Again,
Matt

Last edited by mrm5102; 08-16-2012 at 04:04 PM..
# 9  
Old 08-16-2012
I think the escape-character (\033) is represented in different ways in different programs. "\033" is one alternative and "^[" is another, but it's still the same character, e.g. "vi" is showing it as "^[".
I'm using bash on FreeBSD, and if I want to take away VT100 escape sequences with sed I use this
Code:
sed -E 's/'$(echo -e "\033")'\[[0-9;]+m//g'

The "escape codes" can be combined so "\033[1;4m" turns on both bold and underline. In the answer from Chubler with perl there are some other escape sequences that are taken away, I don't know anything about them, but one is changing windows linefeed to unix linefeed - I think.

Last edited by 244an; 08-16-2012 at 10:16 PM..
# 10  
Old 08-17-2012
Hey 244an, thanks for the reply!

That's pretty cool... Thanks!
So that part of the REGEX that has $(echo -e "\033"), does this change the Escape Sequence to whatever that local machine uses..?


I came up with this sed command below that seems to work. The problem that I ran into was that some of the servers that will contain this script
when I'm done are pretty old... Most of the older ones are AIX 5-or-6 and after checking out their sed command's man page I realized it doesn't
have the "-r" (and I just checked for your "-E" option) or the "-E" option either...
So this is what I came up with:
Code:
while read line
 do

        # This matches "^[[" then any single digit number followed by and "m"...
        echo "$line" | sed 's/\^\[\[[0-9]m//g'

done < $myFile

Thanks again for the info!


Thanks Again,
Matt
# 11  
Old 08-17-2012
Quote:
Originally Posted by mrm5102
...
So that part of the REGEX that has $(echo -e "\033"), does this change the Escape Sequence to whatever that local machine uses..?
No, it doesn't change to anything, the character with ASCII oct 033 is escape (ESC), the same on all OS. How this character is shown can differ though. As an example it's shown as ^[ in "vi". And that is one character, as you will notice if you move the cursor in "vi" through e.g. ^[[0m - 4 characters.
I don't understand how you can do this with the regex \^\[\[[0-9]m, the only explanation I have is that someone have done a copy-paste when it have been shown as e.g. ^[[1m on the screen. But in that case it's no longer a VT100 escape sequence, and I don't think it will do any underline or bold if you don't replace them. I mean the one character ESC is sometimes shown as ^[, but if you type the two characters ^[ it doesn't mean ESC.
# 12  
Old 08-20-2012
Hey 244an, thanks for the reply...
Thanks for the insight..!

Sorry I'm not exactly sure what your asking?
Are you saying that I "shouldn't" be able to remove these character sequences with the REGEX,
because "^[" is NOT literally a "^" or a "[", but "literally" an "ESC"...?

If that's what your saying, then I'm not sure why it would be working then..? Maybe it's the way I'm
writing the output to the file...


EDIT:
Ok... I see what your saying now. I just checked the output file in VI and you were right about the "^[", if
I scroll through the file it skips over the "^[" as if it were one character. I see what you mean by it being
weird that it actually works.!?

Maybe it has something to do with the "cat -v" command. The "-v" or aka "--show-nonprinting" will use ^
and M- notation, except for LFD and TAB. I'm assuming this probably is the reason why it's working..?

Here is my code for reading the file and removing those sequences:
Code:
EXPECT_OUTPUT="/path/to/output/file.txt"

IFS='
'
### If the output file exists, then read in the Output file and split it line-by-line...
if [ -e $EXPECT_OUTPUT ]
 then
    TEMP_Lines=( $(cat -v "$EXPECT_OUTPUT" | sed 's/\^\[\[[0-9]m//g') )
fi

Now from the command line if I run "cat myOutputFile.txt" I get:
Quote:
DATE: Monday 08-20-2012
TIME: 10:15 AM
REMOTE_HOST: myHost-vm (192.168.5.101)

Getting the Status of the NRPE Daemon:
- The NRPE Daemon is Running with PID=2532
Then running "cat -v myOutputFile.txt":
Quote:
^[[1mDATE:^[[0m Monday 08-20-2012
^[[1mTIME:^[[0m 10:15 AM
^[[1mREMOTE_HOST:^[[0m myHost-vm (192.168.5.101)

Getting the Status of the NRPE Daemon:
- The NRPE Daemon is Running with ^[[1mPID=2532^[[0m
Soo I'm not exactly sure why it's working, but I just know i'm glad it does lol...
But anyway thanks again for your reply...


Thanks Again,
Matt
# 13  
Old 08-21-2012
Hey All,

I just accidentally came across this page while Googling for something totally unrelated (GO FIGURE!!!)...
This link has some pretty cool stuff in terms of removing those color codes from your output...

Remove color codes (special characters) with sed | commandlinefu.com

Hope that may help someone!


Thanks,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Escaping backslash and asterisk in egrep to match "\*"

So far what i've got is egrep '^(\\)\*$'No luck. I've searched the web and not much luck. I know about the escape character \ but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks! (8 Replies)
Discussion started by: matthewfs
8 Replies

3. Shell Programming and Scripting

sed command escaping backslash "/"

Hello friends/'unix experts', i have a file as below cat sample.txt satish /rakesh/ sandhya /sandeep/ i have to replace /rakesh/ with rakesh, how can i do it with sed, i tried below code but its throwing errors sed -e 's/'"\(/rakesh/)\"'/\1rakesh/g' sample.txt ... (1 Reply)
Discussion started by: only4satish
1 Replies

4. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

5. AIX

echo $varibla | mail -s "subject" "xxx@xxx.com" not ruuning as expected

Hi Folks, As per the subject, the following command is not working as expected. echo $variable | mail -s "subject" "xxx@xxx.com" Could anyone figure it out whats wrong with this. I am using AIX box. Regards, (2 Replies)
Discussion started by: gjarms
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

9. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

10. Shell Programming and Scripting

Interpretation of "echo $-"

I am running Solaris 10. Can someone help me interpret the output from "echo $-"? Interactively, the output is: ism From within a cron job, the output is: xh Why are they different? Can someone point me to the appropriate documentation? Thanks, J (1 Reply)
Discussion started by: shew01
1 Replies
Login or Register to Ask a Question