简体   繁体   English

awk +从双引号定义的行中打印字段

[英]awk + print the field from line that defined with double quotes

how to print the third field from line that defined with double quotes 如何从用双引号定义的行中打印第三个字段

for example 例如

echo activate.bash /home/adm/run.sh "run on machine with eth2 active" "94061" | awk '{print $3}'

I expected that the awk will print the third filed in the double quotes 我希望awk将打印双引号中的第三个文件

 run on machine with eth2 active

but its print 但它的印刷品

 run

so what's I need to fix in my ksh syntax? 所以我需要在ksh语法中解决什么?

other example 其他例子

echo 123 123 "1 2 3 4 5" | awk '{print $3}'

should print 应该打印

1 2 3 4 5

You can use custom field separator in awk like this: 您可以在awk中使用自定义字段分隔符,如下所示:

s='activate.bash /home/adm/run.sh "run on machine with eth2 active" "94061"'
echo "$s" | awk -F '"' '{print $2}'
run on machine with eth2 active

PS: Better to use: PS:更好地使用:

awk -F '"' '{print $2}' <<< "$s"

Working Code Demo 工作代码演示

Let's take the following example: 让我们来看下面的例子:

 cat numbers
 123 123 "run on machine with eth2 active" "94061"


cat numbers | awk -F '"' 'BEGIN{OFS=" ";} {print $2;}'

OUTPUT: run on machine with eth2 active 输出:在启用了eth2的计算机上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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