在xargs结构中使用awk :当我在xargs中输入awk时,获取不同的输出--
step1
~/waste/dgx-users2.log | head -n2
node1
node2step2
cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo $1 && kubectl get pods --field-selector=spec.nodeName=$1,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中的test-s,所以在下面过滤
cat ~/waste/dgx-users2.log | head -n2 | xargs -l1 -- sh -c 'echo $1 && kubectl get pods --field-selector=spec.nodeName=$1,status.phase=Running --all-namespaces --no-headers' -- | awk '$1 ~ /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 $1 && kubectl get pods --field-selector=spec.nodeName=$1,status.phase=Running --all-namespaces --no-headers | awk "$1 ~ /test-*/ && /Running/ {print}"' --
node1
node2我没有得到相同的输出..所以我想需要调整一些语法..我的预期输出应该是。
我的目标是打印节点信息和输出,这样我就知道pod在哪个节点上运行,如下所示: node1没有任何内容,而node2有一个pod。
node1
node2
test-s test-pod 3/3 Running 0 6d发布于 2020-03-31 05:12:06
由于awk表达式中的$1由sh的内部副本在双引号上下文中解析,因此需要对其进行转义。这就是:
head -n2 <~/waste/dgx-users2.log | xargs -l1 -- sh -c '
echo "$1" &&
kubectl get pods --field-selector=spec.nodeName="$1",status.phase=Running \
--all-namespaces --no-headers |
awk "\$1 ~ /test-*/ && /Running/ {print}"
' --https://stackoverflow.com/questions/60939509
复制相似问题