简体   繁体   中英

Use AWK to print lines that have a certain string followed by a number greater than 10000

I have a file that looks something like this:

column1 column2 column3 column4 column5 column6 Warn=3000
column1 column2 column3 column4 column5 Warn=200
column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 Warn=100
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 Warn=15000

As it can be seen, the number of fields is changing on each line, but the last field is always "Warn=" followed by a number. I basically want to print all the lines where the "Warn=" string is followed by a number greater than 10000 and sort them from highest number to lowest.

So the result should look like this:

column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 column5 Warn=15000

So far I've been able to somewhat achieve what I want by using grep, awk and sort:

grep -E 'Warn=[0-9]{5}' file.txt | awk '{ print $NF, $0 }' | sort -rn -k1 | sed 's/Warn=[0-9]* //'

Is there an easier way of doing it just with awk?

EDIT: Since OP mentioned that Input_file could have = for other fields too, could you please try following then.

awk '{split($NF,array,"=")} array[1]=="Warn" && array[2]>10000' Input_file | sort -t'=' -k2rn

Could you please try following(considering that actual Input_file is same as shown samples).

awk -F' |=' '$(NF-1)=="Warn" && $NF>10000'  Input_file

OR to sort with value use:

awk -F' |=' '$(NF-1)=="Warn" && $NF>10000' Input_file | sort -t'=' -k2rn

Output will be as follows.

column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 Warn=15000

With GNU awk for sorted_in:

$ awk -F'=' -v OFS='\t' '$NF>10000{a[NR]=$NF; b[NR]=$0} END{PROCINFO["sorted_in"]="@val_num_desc"; for (i in a) print b[i]}' file
column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 Warn=15000

or with any awk plus sort + cut:

$ awk -F'=' -v OFS='\t' '$NF>10000{print $NF, $0}' file | sort -nr | cut -f2-
column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 Warn=15000

Using Perl one-liner

> cat warn.txt
column1 column2 column3 column4 column5 column6 Warn=3000
column1 column2 column3 column4 column5 Warn=200
column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 Warn=100
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 Warn=15000

> perl -ne ' { if(m/(\d+)$/s && $1 >  10000) { $warn{$1}=$_; } }  END { foreach $key(reverse sort keys %warn) { print "$warn{$key}" } } ' warn.txt
column1 column2 column3 column4 column5 column6 column7 Warn=40000
column1 column2 column3 column4 column5 Warn=20000
column1 column2 column3 column4 Warn=15000

>

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