It's best to explain with an example what I want to achieve.
I have this kubectl output:
app-bf7c5cdfb-x8thq 3/3 Running 0 2h
app-bf7c5cdfb-xc9nf 3/3 Running 0 2h
app-bf7c5cdfb-xwlzh 2/3 Running 0 1h
app-bf7c5cdfb-xxt46 3/3 Running 0 2h
app-bf7c5cdfb-z9s2t 2/3 Running 0 7m
app-bf7c5cdfb-zmpzs 3/3 Running 0 2h
app-bf7c5cdfb-ztfx5 3/3 Running 0 2h
I want to grep for each line, where the ready pods are not equal to the total pods resulting in:
app-bf7c5cdfb-xwlzh 2/3 Running 0 1h
app-bf7c5cdfb-z9s2t 2/3 Running 0 7m
Is there a simple way to do that? My approach is writing a function like this:
function kubeNotReady {
unset tmp
while :
do
read line
[[ $line == "" ]] && tmp="${tmp:0:$((${#tmp}-1))}" && break
total=`echo "$line" | sed -E "s|.*[[:digit:]]*/([[:digit:]]*).*|\1|"`
match=`echo "$line" | grep -v "$total/$total"`
if [ "${#match}" -gt "0" ]; then
tmp="$tmp$match"$'\n'
fi
done
echo "$tmp"
}
kubectl get po | kubeNotReady
Here is a grep which is something like what you want.
echo "4/5" | grep -vE "^(.+)/\1quot;
That handles just the fraction. To make it handle your provided example simply filter out everything apart from the 2nd word before passing it to the grep I provided.
If AWK
is your option, please try the following:
awk '{split($2, a, "/"); if (a[1] != a[2]) print}' out.txt
As an alternative, you can say with bash
as:
while IFS= read -r line; do
[[ $line =~ [[:blank:]]([[:digit:]]+)/([[:digit:]]+)[[:blank:]] ]] && (( ${BASH_REMATCH[1]} != ${BASH_REMATCH[2]} )) && echo "$line"
done < out.txt
Hope this helps.