使用 awk insdie xargs 构造

using awk insdie xargs construct

在 xargs 构造中使用 awk:当我尝试在 xargs 中引入 awk 时得到不同的输出 - -

第一步

 ~/waste/dgx-users2.log | head -n2
node1
node2

第 2 步

cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo  && kubectl get pods --field-selector=spec.nodeName=,status.phase=Running --all-namespaces --no-headers' --

node1
node2
test-s test-s-pod 3/3   Running   0     6d1h
dummy-s test-s-pod 1/1 Running 0 5d9h

但我只想列出 $1 中的测试,所以在下面过滤

cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo  && kubectl get pods --field-selector=spec.nodeName=,status.phase=Running --all-namespaces --no-headers' -- | awk ' ~ /test-*/ && /Running/ {print}'
test-s test-pod                       3/3   Running   0     6d

以上都很好,但有没有办法可以将 awk 拉入 xargs — ?我在下面尝试过,但它缺少在我的预期输出中显示的最后一行输出。

cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo  && kubectl get pods --field-selector=spec.nodeName=,status.phase=Running --all-namespaces --no-headers | awk " ~ /test-*/ && /Running/ {print}"' --
node1
node2

我没有得到相同的输出..所以我想需要调整一些语法.. 我的预期输出应该是。

我的目标是打印该节点信息以及输出,以便我知道 pod 在哪个节点上 运行 如下所示,node1 什么都没有,node2 有一个 pod。

node1
node2
test-s test-pod                       3/3   Running   0     6d

因为 awk 表达式中的 </code> 被 <code>sh 的内部副本在双引号上下文中解析,所以需要对其进行转义。即:

head -n2 <~/waste/dgx-users2.log | xargs -l1 -- sh -c '
    echo "" &&
    kubectl get pods --field-selector=spec.nodeName="",status.phase=Running \
                     --all-namespaces --no-headers |
    awk "$1 ~ /test-*/ && /Running/ {print}"
  ' --