简体   繁体   English

如何将包含引号的命令的输出作为 awk 变量传递?

[英]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:这里的变化是:

  1. Use $() to execute date and get the output back for the variable d .使用$()执行 date 并获取变量d的输出。
  2. Using single quotes for the date format so you don't have double quotes in double quotesdate格式使用单引号,这样双引号中就没有双引号
  3. Using 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM