Perl 单行单引号 [英] Perl one-liner with single quote

查看:67
本文介绍了Perl 单行单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Perl 单行来创建 SQL 语句,但我无法包含单引号.

I was 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 first filed 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.

它正在打印带有打印语句的单引号,但是如果我为变量赋值并打印它就不起作用.

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"

你能帮我理解我们如何在 Perl 单行中使用单引号和双引号吗?

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

推荐答案

不能单独使用单引号.您需要使用 '\'' 正确转义它们 这有效:

You can't use single quotes alone. You need to escape them correctly using '\'' This works:

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

这篇关于Perl 单行单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆