Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl one-liner with single quote

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.

  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 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?

like image 482
sfgroups Avatar asked Feb 24 '11 14:02

sfgroups


People also ask

How do you handle a single quote in a string?

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”.

How do you quote in Perl?

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.


1 Answers

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.

like image 184
Eugene Yarmash Avatar answered Dec 01 '22 22:12

Eugene Yarmash