简体   繁体   中英

Bash, compare the a result awk '{print $1} with something and return awk '{print$2} if true

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 ?

您可以完全在awk中执行此操作

awk '$1 > 100 { print $2 }' "$CFILE"
{m,g}awk '$!NF = $(NF *= 100<+$_)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM