[英]How do I pass the output of a command that contains quotes as an awk variable?
希望使用 awk 设置日期变量,但无法正确使用引用或语法!
awk -v c=$i -v d="date +\"%D %r %Z\"" '{print d c }'
Consider instead:考虑一下:
awk -v d="$(date +'%D %r %Z')" 'BEGIN{print d}'
The changes here are:这里的变化是:
$()
to execute date and get the output back for the variable d
.使用$()
执行 date 并获取变量d
的输出。date
format so you don't have double quotes in double quotes对date
格式使用单引号,这样双引号中就没有双引号BEGIN
to execute the print
statement in awk.在 awk 中使用BEGIN
执行print
语句。 This isn't necessary if you are feeding a file or stdin to awk for it to read records.如果您将文件或标准输入提供给 awk 以便它读取记录,则这不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.