Disabling Backslash Interpretation with "echo -E"?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Disabling Backslash Interpretation with "echo -E"?
# 1  
Old 08-15-2012
Disabling Backslash Interpretation with "echo -E"?

Hello All,


In a Bash Script I'm writing I have a section where I loop through a text file that was
outputted by another script. In the text file some of the strings in there are enclosed with
the BOLD "character sequences" (i.e. "\033[1m" ) and the "OFF" sequence (i.e. "\033[0m" ). Now I
am reading the file line-by-line in a while loop and saving each line into an element in an array.

What I would like to do is remove/disable these backslah escapes while saving the line from the
file into the Array element.

But it's weird, because if I run this command:
echo -E "\033[1m Hello World \033[0m"

The Output is:
\033[1m Hello World \033[0m

And running it with "-e" instead, prints:
Hello World

Here is a part of my while loop and I was hoping that it would diable those backslash escapes but it doesn't.
Code:
    while read line
     do
        if [[ $line =~ $PATTERN ]]
         then
            FILE_Lines[$x]=$(echo -E "$line")
            x=$(($x+1))
        fi
    done < $outputFile

So is there another way to read in the file or while saving the line to remove those sequences..?
I also tried adding a "sed 's/\\033\[1m//g'" to the end of the line in the while loop that assigns the line to the
FILE_Lines Array, but that didn't do anything either... I've also tried every different combination of quotes,
single and double, and still nothing...


If anyone has any suggestions, that would be great!


Thanks in Advance,
Matt
# 2  
Old 08-15-2012
Try this:
Code:
$ line="\033[1m Hello World \033[0m"
$ echo $line | sed 's/\\033\[1m \(.\{1,\}\) \\033\[0m/\1/'
Hello World

# 3  
Old 08-15-2012
I did a search for "Strip VT100 escape sequences" and came up with this perl code (thanks Anonymous Monk):

Code:
perl -pe  's/\e\[[\d,\s]*[a-zA-Z]//g; s/\e\][\d];//g; s/\r\n/\n/g; s/[\000-\011]//g; s/[\013-\037]//g'

so..
Code:
FILE_Lines[$x]=$(echo -e "$line"|perl -pe  's/\e\[[\d,\s]*[a-zA-Z]//g; s/\e\][\d];//g; s/\r\n/\n/g; s/[\000-\011]//g; s/[\013-\037]//g')

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 08-16-2012
Hey Guys, thank you both for the replies.

I'll give those a try and post back....
I had no idea though that they were called VT100 Escape Sequences... Good to know!


Thanks Again,
Matt

---------- Post updated at 09:44 AM ---------- Previous update was at 09:24 AM ----------

Hey spacebar, I gave that a try and it didn't seem to work...
Probably some character missing or something like that within the REGEX Pattern somewhere...


Hey Chubler_XL thanks again for your suggestion!
Perfect! Thanks that did the trick! That's gotta be one of the longest REGEX's I've ever seen lol...


Many thanks...!


Thanks Again,
Matt
# 5  
Old 08-16-2012
Quote:
Originally Posted by mrm5102
Perfect! Thanks that did the trick! That's gotta be one of the longest REGEX's I've ever seen lol...
Take a closer look; it's five little regexes.
# 6  
Old 08-16-2012
Hey Corona, thanks for the reply...

Yeah funny you say that, I actually just noticed that a little while before I read your post... Thanks!


UPDATE:
So I'm guessing I ran into a little Perl version problem on some of our older servers...
When I ran the REGEX on the machine I'm testing this on, it worked perfectly... But when I transferred it over
to another server running Perl v5.8.2 and another with v5.8.8 it didn't do anything to the "strings" at all.

So what I did was, I tried to simplify the REGEX, because I'm guessing it matched ANY/ALL V100 Character
Sequences, but I ONLY needed to look out for the ones for BOLD "\033[1m", and the one for OFF "\033[0m"
and possibly the one for UNDERLINE "\033[4m"...

Strangely though, those escape sequences above are how you would enter them into a bash script, like this:
i.e. --> echo -e "\033[1m THIS PRINTS BOLD \033[0m"

Then if I redirect the echo statement into a txt file, they show like this instead of what's above...
i.e. --> "^[[1m THIS PRINTS BOLD ^[[0m"


So here's my new REGEX (also I changed the while loop to just a 'cat' command):
Code:
outputFile="/usr/local/test/Output.txt"

### Split the output of the 'cat' command on newlines, and insert it into the array FILE_Lines...
IFS='
'
FILE_Lines=($(cat $outputFile | perl -pe 's/\^\[\[(1|0|4)m//g'))

for (( x=0; x<=${#NRPE_Lines[@]}; x++ ))
 do
    echo ${NRPE_Lines[$x]}
done

So far this seems to remove ALL the escape sequences from the file, so I guess
I'll use this one until or if/when something else goes wrong lol...



Thanks again you guys for your suggestions... Much appreciated!!


Thanks Again,
Matt
# 7  
Old 08-16-2012
You get a useless use of cat award.

All your code amounts to is
Code:
perl -pe 's/\^\[\[(1|0|4)m//g' < file

Do you really need to store the entire file into an array? Why not just read it line-by-line?

Code:
perl -pe 's/\^\[\[(1|0|4)m//g' < file | while read LINE
do
        echo "do something"
done

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