linux - 在bash中获取匹配行的第一列
问题描述
我从一个 shell 命令得到一个输出,并且想将匹配行的第一列合并到第二个命令中。这是我所拥有的,并且有效:
kubectl get pods -n system | while read string; do
if [[ "$string" == my-pod-* && "$string" == *Running* ]]; then
# echo $string
read -ra ADDR <<< "$string"
echo
echo "--- Reading logs for ${ADDR[0]} ---"
# desired output: kubectl -n system logs my-pod-123 --tail=5 -f
kubectl -n system logs ${ADDR[0]} --tail=5 -f
fi
done
第一个命令的输出如下所示:
name status namespace running
my-pod-123 Running system 4h31m #<<I want this one
another-pod-5 Running system 5h15m
my-pod-023 Terminating system 8h05m
鉴于输出将仅包含一个匹配项,是否有更短的方法可以做到这一点而无需像这样循环?提前感谢您帮助我提高我的 Bash 技能,因为这看起来很笨拙。
解决方案
怎么样grep
?
wanted=$(kubectl get pods -n system | grep 'my-pod-.*Running')
可以同时做错误检查:
if ! wanted=$(kubectl get pods -n system | grep 'my-pod-.*Running'); then
echo "Error: no running my-pods" >&2
fi
推荐阅读
- python - 如何将json中的数据存储在sql表中(python)
- java - 我的测试注释不起作用,eclipse 要求主要方法
- spring - 如何使用 PactVerificationSpringProvider 和 Junit5 为协议提供程序测试添加标头?
- c++ - UE4 CableComponent 因未知原因未渲染
- python - 如何在 Pandas Python 上设置多列?
- java - 如何检查用户是否存在于 Radius 服务器中
- laravel - Laravel 验证:在不需要字段时排除大小检查
- javascript - Javascript 或 Ramda 按属性转换 JSON
- python - 如何从 Spark 结构化流写入 RDMS?foreach 行里面是什么?
- java - 将 apache cxf 升级到 3.4.1 导致错误