LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Handle escape characters in a string (https://www.linuxquestions.org/questions/programming-9/handle-escape-characters-in-a-string-176530/)

Helene 04-30-2004 10:36 PM

Handle escape characters in a string
 
echo -n "Please enter filename(s) in regular expression: "

read -r option # assume that the user typed in: .*\.txt
echo $option # prints out .*\.txt

How can I change the content of $option to be .*\\.txt?
Why can't I just do:
echo $option | tr '\' '\\' ?

I appreciate any help!
-Helene

slakmagik 04-30-2004 10:57 PM

Code:

#!/bin/bash
echo -n "Please enter filename(s) in regular expression: "
read -r option
echo $option | sed 's/\\/\\\\/'

Not sure what you're doing, but will that work? I tend not to use tr, but sed will do it if you escape the backslash and then escape both replacements.

Helene 04-30-2004 11:14 PM

Excellent, digiot! Now I'm soon ready to deliver my Uni assignment ;)

slakmagik 04-30-2004 11:20 PM

Uni assignment? Argh. I can usually tell when it's a homework thing, but I didn't realize it in this case. You do know you're not supposed to ask for homework help, right? I mean, you can ask about principles and I or others would offer hints and principles, but just straight questions and answers are kind of a no-no.

Ah well, bit past the point now. Good luck with the classwork, anyway.

Helene 05-01-2004 04:23 AM

Well, I'm only asking about principles, digiot. I would never have asked for direct answers when doing assignments! :study: If you are still willing to help me, I have one more question:

test=`echo $option | sed 's/\\/\\\\/''`

This results in Unterminated `s' command. Why?
I did find another way aronud (not pretty), but I'm just curious :rolleyes:

slakmagik 05-01-2004 02:16 PM

Actually, what you posted has two single quotes, so gets 'unexpected EOF' for me, but
Code:

test=`echo $option | sed 's/\\/\\\\/'`
does get 'unterminated'.

The original 'backticks' method is treated differently and the newer way works:
Code:

test=$(echo $option | sed 's/\\/\\\\/')
Near as I can figure, the backtick method is resulting in the backslashes getting 'swallowed', and maybe the slashes being treated by sed as literal - so it's still waiting on a 'special' slash to terminate the substitution.
Code:

~
990>> echo `echo /\\/\\\\/`                                                 
//\/

~
991>> echo $(echo /\\/\\\\/)                                                 
/\/\\/

In the first, bash is passing to sed what looks like 'substitute nothing with a literal slash and...' whereas the second is 'replace a slash with two'. Now, what bash is passing to sed and what echo is outputting in 990/991 are two different things but I still think that's basically what's going on. Could well be wrong, though. It's not that I'm not willing to help, but I just don't really know.

Just out of curiosity, what was the way around? (If you don't want to say, that's okay. I, too, know the shame of ugly workarounds. :) )

Helene 05-01-2004 11:24 PM

This one seems to be fine! I didn't know/think of using $().
test=$(echo $option | sed 's/\\/\\\\/')

And yes, I had a typo. I didn't mean to write two single quotes, only one.

My ugly solution was to write the variable to a file, and write it back into a variable using awk...
echo $option | sed 's/\\/\\\\/' > tmpFile ..... :rolleyes:

slakmagik 05-01-2004 11:43 PM

Yeah, I try to avoid temp files but sometimes it does seem like the least troublesome route. :) Glad the other suits.


All times are GMT -5. The time now is 01:17 AM.