bash - 将 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}' --
解决方案
在“如何在另一个单引号短语中使用 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}"
' --
推荐阅读
- mono.cecil - 在 Mono.Cecil 中解析对 TypeDefinition 的 TypeReference 失败并出现程序集解析错误
- wagtail - 如何添加一个类别片段,用于不同的片段
- airflow - 气流UnicodeDecodeError:'ascii'编解码器无法解码字节0xe2
- python - 使用 zip 在数据框中编织列
- excel - 计算我们增加给定日期范围的次数
- ffmpeg - 使用 libav 将 RGBA 帧编码为 MP4 但输出一团糟
- javascript - 如何在 React 中将重复的 JSX 抽象为高阶组件
- excel - 通过 VBA 将文件嵌入 Excel 会损坏文件
- graph-algorithm - 这个 LCA 算法可以吗,在其他地方使用过吗?
- sql - 尝试查找有行和无行的视图