Optimize code in single line

Dear team,

The below code is to check if number of lines in file greater than two then status is not ok else status is ok.
I need to optimize it in single line code in awk or sed if possible.

Pls help

cat LOTCCLUS_stat
count=`cat LOTCCLUS_stat | wc -l`
if [[ $count -gt 2 ]]; then

echo "##### THE LOTC cluster state IS NOK ##########"
else
echo "##### THE LOTC cluster state IS OK  ##########"
fi

The file as below

Consolidated to 3 lines of shell code:

cat LOTCCLUS_stat
if [[ `wc -l < LOTCCLUS_stat` -gt 2 ]]; then ok="NOK"; else ok="OK "; fi
echo "##### THE LOTC cluster state is $ok ##########"

Hi
Such a crocodile

echo "##### THE LOTC cluster state IS $((($(<LOTCCLUS_stat wc -l) > 2)) && echo N)OK ##########"

Why single line? Another croc to try:

awk 'NR>2{e="N"; exit 1} END{printf "##### THE LOTC cluster state IS %sOK ##########\n",e}' file
1 Like

Optimization does not mean condensing several readable single commands into a less readable single line - which can be harder to maintain, say, in six months. When you have forgotten about it. And you have to come back for other changes - then you wonder what this 'optimized' code really does.

Crocodile indeed.

3 Likes