[英]Select rows from a text file by a matching pattern in one column
I have a file in the following format. 我有以下格式的文件。
Table_name Value
Employee 0
student 50
Payroll 0
animals 20
I need to fetch the entire row in which the value is non-zero. 我需要获取值不为零的整行。
The expected output would be 预期的输出将是
student 50
animals 20
If the value is zero for all the rows, I should get a mail alert like "all values are zero". 如果所有行的值均为零,则应收到类似“所有值均为零”的邮件警报。
This can work: 这可以工作:
$ awk '$2' file
Table_name Value
student 50
animals 20
If the 2th field is not 0
then the condition is evaluated as true - note that {print $0}
is the default block in awk
so it can be omitted if this is the only action you want to perform. 如果第二个字段不为0
则条件为true-注意{print $0}
是awk
的默认块,因此,如果这是您要执行的唯一操作,则可以将其忽略。
Regarding the mail alert, I think you'd better show some of your code to have a refer point. 关于邮件警报,我认为您最好显示一些代码以具有参考点。
Following awk should meet both of your requirements: 执行awk后,应该同时满足您的两个要求:
awk 'NR>1 && $2 {nz=1;print}; END{if (!nz) print "all zeroes, send email"}' file
print "all zeroes, send email"
with your mail sending command. 您只需要用邮件发送命令替换print "all zeroes, send email"
。 为什么不Perl:
perl -ane 'print if($.!=1 && $F[1]!=0)' your_file
非常简单地使用awk,
awk -F " " '{if($2 > 0) print $0}' your_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.