简体   繁体   English

使用AWK打印具有特定字符串后跟大于10000的数字的行

[英]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. 可以看出,每一行的字段数都在变化,但是最后一个字段始终是“ Warn =”,后跟一个数字。 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. 我基本上想打印“ Warn =”字符串后跟大于10000的所有行,并按从高到低的顺序对它们进行排序。

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,awk和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? 是否只有使用awk才能更轻松地完成此操作?

EDIT: Since OP mentioned that Input_file could have = for other fields too, could you please try following then. 编辑:由于OP提到Input_file也可以对其他字段也有= ,请您再尝试以下内容。

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). 您能否请尝试以下操作(考虑到实际的Input_file与所示示例相同)。

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: 使用GNU awk进行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加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 使用Perl单线

> 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

>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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