我有以下格式的文件。通過一列中的匹配模式從文本文件中選擇行
Table_name Value
Employee 0
student 50
Payroll 0
animals 20
我需要獲取值爲非零的整個行。
預期產出將是
student 50
animals 20
如果該值是零的所有行,我應該得到郵件提示,如「所有值都爲零」。
我有以下格式的文件。通過一列中的匹配模式從文本文件中選擇行
Table_name Value
Employee 0
student 50
Payroll 0
animals 20
我需要獲取值爲非零的整個行。
預期產出將是
student 50
animals 20
如果該值是零的所有行,我應該得到郵件提示,如「所有值都爲零」。
爲什麼不使用Perl:
perl -ane 'print if($.!=1 && $F[1]!=0)' your_file
繼AWK應同時滿足您的要求:
awk 'NR>1 && $2 {nz=1;print}; END{if (!nz) print "all zeroes, send email"}' file
print "all zeroes, send email"
。太棒了!非常感謝:) – Vignesh
很簡單使用awk,
awk -F " " '{if($2 > 0) print $0}' your_file
這很好,但請注意,您正在使用一些已在默認設置下的選項:'-F「」','print $ 0'。 – fedorqui
代碼GNU sed:
$sed '/\S+\s+[0]+\b/d' file Table_name Value student 50 animals 20 $sed -r '/\S+\s+([1-9]|[0]+[1-9])/!d' file student 50 animals 20
我只有Unix環境 – Vignesh
Perl是標準Unix環境的一部分。 –