Home > Enterprise >  Bash, compare the a result awk '{print $1} with something and return awk '{print$2} if tru
Bash, compare the a result awk '{print $1} with something and return awk '{print$2} if tru

Time:06-30

My File Looks like this:

    790 45.61.188.53
    494 45.61.188.95

I need to compare the first one for example 790 with something else and then print the second one.

So my code actually look like this:

c=$(cat $IPS | sed '/^$/d' | sort -rn | uniq -c | sort -rn | awk '{print $1}' | head -5 >> $CFILE)
i=$(cat $IPS | sed '/^$/d' | sort -rn | uniq -c | sort -rn | awk '{print $2}' |head -5)


for count in `cat $CFILE`
do
    if (( $count > 100 ));then
        echo "$i"
    fi
done

But the return output is :

45.61.188.53
45.61.188.95
20.230.214.19
45.61.187.215
78.142.63.3
45.61.188.53
45.61.188.95
20.230.214.19
45.61.187.215
78.142.63.3
45.61.188.53
45.61.188.95
20.230.214.19
45.61.187.215
78.142.63.3
45.61.188.53
45.61.188.95
20.230.214.19
45.61.187.215
78.142.63.3

and so on

I assume that this is normal behavior for the for loop but I cannot understand how can I do it using something different. Maybe arrays would be useful ?

CodePudding user response:

You can do this entirely in awk

awk '$1 > 100 { print $2 }' "$CFILE"
  • Related