Perl single-line with single quote

I used single-line Perl to create an SQL statement, but I cannot include single quotes.

This is what I want: first download and add quotation marks to it.

echo "a,b" | perl -F',' -lane 'print $F[0];' 'a' 

I tried several different ways, but this did not work for me.

1.

 echo "a,b" | perl -F',' -lane 'print qq('$F[0]');' [0] 

2.

 echo "a,b" | perl -F',' -lane 'print q('$F[0]');' [0] 

Here is another interesting one.

It prints a single quote with a print statement, but if I assign a value to a variable and do not print it, it will not work.

 perl -lwe "print q( i'am );" i'am perl -lwe "$b=q( didn't ); print $b" 

Can you help me understand how we can use single and double quotes in single-line Perl?

+2
source share
4 answers

You cannot use single quotes alone. You need to avoid them correctly using '\'' This works:

 $ echo "a,b" | perl -F',' -lane 'print "'\''$F[0]'\''";' 'a' 
+16
source

You need to find out how your shell processes quotes.
I would just use the ASCII value for ' :

 echo "a,b" | perl -F',' -lane 'print "$F[0]\047";' a' 
Operators

q// and qq// can also be useful in the same layer.

+9
source

Use a variable with an octal value:

 echo "a,b" | perl -F',' -lane '$sq="\047"; print "$sq$F[0]$sq";' 

In addition, a small modification to your attempt # 1 will work:

 echo "a,b" | perl -F',' -lane "print qq{'\$F[0]'};" 

It uses double quotes for external dialing and escapes the dollar sign to prevent shell interpretation.

+2
source

Placing the script in in double quotes instead of single quotes will allow you to use single quotes inside the script without having to run or use ANSI sequences to represent a single quote. This is perhaps the most effective and easy to read solution.

0
source

Source: https://habr.com/ru/post/1389320/


All Articles