Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Why is the escaping of double quotes are lost in this case?

$ cat foo.txt   
This is a "very good" text worth AMOUNT dollars    
$ cat full_story.txt   
This is about money:  
STORY  

Testing it with the following:

VAR=$(cat foo.txt)                                                                                         
TOTAL=$(cat full_story.txt)  
echo "$TOTAL" | perl -pe "s/STORY/$VAR/g"  

Result:

This is about money:  
This is a "very good" text worth AMOUNT dollars  

The escape of double quotes got lost. I was expecting:

This is about money:  
This is a "very good" text worth AMOUNT dollars  

How can I preserve the escapes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
998 views
Welcome To Ask or Share your Answers For Others

1 Answer

The problem is that perl parses backslash escapes in an explicit replacement string (not a perl variable), and hence a " is parsed into a". For example:

$ echo "A STORY" | perl -pe 's/STORY/"Hello"/'
A "Hello"

(Note that the Bash variable $VAR does not become a perl variable $VAR , but rather a constant string.) So you need to escape the backslashes like this in constant string:

$ echo "A STORY" | perl -pe 's/STORY/"Hello"/' 
A "Hello"

You can work around the issue by transferring the Bash variable $VAR into a perl variable $VAR by using the -s switch to perl like this:

echo "$TOTAL" | perl -spe 's/STORY/$VAR/g' -- -VAR="$VAR"

Ouput:

This is about money:  
This is a "very good" text worth AMOUNT dollars

Explanation:

  • -s enables switch parsing for user defined switches on the perl command line. Any switch found there is removed from @ARGV and sets the corresponding variable in the Perl program.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...