I use a Perl one-liner to create an SQL statement, but I am not able to include single quotes.
This is what I want: Take the first field and add quotes to it.
echo "a,b" | perl -F',' -lane 'print $F[0];'
'a'
I tried a few different ways, but it didn't work for me.
echo "a,b" | perl -F',' -lane 'print qq('$F[0]');'
[0]
echo "a,b" | perl -F',' -lane 'print q('$F[0]');'
[0]
Here is another interesting issue.
It is printing a single quote with the print statement, but if I assign a value to the variable and print, it's not working.
perl -lwe "print q( i'am );"
i'am
perl -lwe "$b=q( didn't ); print $b"
How can we use single and double quotes in Perl one-liners?
So, to allow values within single quotes (and some other special characters) to be used within a string, you need to “escape” them. Escaping a character is where you say to the database, “Hey, this character here is part of my string, don't treat it as a special character like you normally would”.
Quoted Strings In Perl, strings can be put in between double-quotes (” “) or in between single-quotes (' '). However, strings defined in single-quotes and those defined in double-quotes are treated differently.
You need to learn how your shell treats quotes.
I would just use the ASCII value for '
:
echo "a,b" | perl -F',' -lane 'print "$F[0]\047";'
a'
q//
and qq//
operators can also be useful in one-liners.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With