首页 > 解决方案 > 将 xargs 与通过管道传输到 awk 的输出一起使用会引发语法错误

问题描述

团队,我阅读了很多帖子,但对我来说不清楚..

我需要了解如何处理这个问题。我让 xargs 进行一些评估,然后通过管道连接到 awk 但出现语法错误

文件日志

node1,
node2,
node3,

使用 xargs 和 awk 失败的表达式

cat file.log | awk -F ',' '{print $1}' | xargs -l1 -- sh -c 'kubectl get pods --all-namespaces --no-headers --field-selector spec.nodeName=$1 | awk "{if ($1 ~ "team-") print $1,$2}"' --

路过这里

cat file.log | awk -F ',' '{print $1}' | xargs -l1 -- sh -c 'kubectl get pods --all-namespaces --no-headers --field-selector spec.nodeName=$1

一旦我添加 awk 部分..我没有得到它。

输出:

awk: line 1: syntax error at or near )

我发现的替代解决方案是将 awk 移出 xargs --。所以下面的作品。但是我怎样才能得到里面的 awk,因为我想打印在 xargs 中使用的 $1 以查看与 awk 正在打印的内容的相关性。

WORKS

--field-selector spec.nodeName=$1' -- | awk '{if ($1 ~ "team-") print $1,$2}'

FAILS

--field-selector spec.nodeName=$1 | awk '{if ($1 ~ "team-") print $1,$2}' --

标签: bashawksedxargs

解决方案


在“如何在另一个单引号短语中使用 awk 的单引号? ”的帮助下弄清楚了。

这对我有用。我awk在 dQuotes 中引用,然后$在里面转义:

cat file.log | 
awk -F ',' '{print $1}' | 
xargs -l1 -- sh -c \
   'kubectl get pods --all-namespaces --no-headers \
                     --field-selector spec.nodeName=$1 | 
    awk "{if (\$1 ~ \"team-\") print \$1,\$2}"
    ' --

推荐阅读