这是命令的输出。
kube-system weave-net-j5mnw 2/2 Running 0 249d
kube-system weave-net-thrlb 2/2 Running 2 249d
kube-system weave-net-tm9ps 2/2 Running 2 249d
logging alert-manager-79fb9f847c-28zk7 1/1 Running 2 2d9h
logging fluentd-billing-0 1/1 Running 0 3d11h我想grep第二列和第三列(这是一个健康检查),如果第三列是0/1、0/2或1/2,就打印出来。简而言之,如果健康检查失败,就打印出第二列和第三列。
请帮帮忙。linux新手入门。
发布于 2020-10-09 10:21:35
cat out | # get the output
tr -s ' ' | # squash whitespace
cut -d ' ' -f 2,3 | # select columns 2 and 3, using space as column seperator
grep -e '0/1' -e '0/2' -e '1/2' # grep only the lines that include a failure发布于 2020-10-09 10:34:25
script.sh遍历file.txt的每一行,从0/2中获取两位数并进行比较。
如果它们是相同的,它将打印通过。
如果它们不相同,则打印失败。
file.txt (在其中添加了一些故障)
kube-system weave-net-j5mnw 0/2 Running 0 249d
kube-system weave-net-thrlb 2/2 Running 2 249d
kube-system weave-net-tm9ps 2/2 Running 2 249d
logging alert-manager-79fb9f847c-28zk7 1/1 Running 2 2d9h
logging fluentd-billing-0 0/1 Running 0 3d11hscript.sh
while read x; do
test_result=$(echo "$x" | cut -d ' ' -f3 | cut -d '/' -f1) # gets the first number in 0/1
test_base=$(echo "$x" | cut -d ' ' -f3 | cut -d '/' -f2) # gets the second number in 0/1
if [[ "$test_result" == "$test_base" ]]; then
echo "$x - PASSED"
else
echo "$x - FAILED!"
fi
done <file.txt # while loop through the file.txt file 结果:
kube-system weave-net-j5mnw 0/2 Running 0 249d - FAILED!
kube-system weave-net-thrlb 2/2 Running 2 249d - PASSED
kube-system weave-net-tm9ps 2/2 Running 2 249d - PASSED
logging alert-manager-79fb9f847c-28zk7 1/1 Running 2 2d9h - PASSED
logging fluentd-billing-0 0/1 Running 0 3d11h - FAILED!发布于 2020-10-09 12:06:19
一个awk程序,如果第3列小于1,则打印第2列和第3列:
command | awk '$3 < 1 {print $2, $3}'https://stackoverflow.com/questions/64272979
复制相似问题