2013-07-04 71 views
0

我有以下格式的文件。通過一列中的匹配模式從文本文件中選擇行

Table_name Value 
Employee 0 
student 50 
Payroll 0 
animals 20 

我需要獲取值爲非零的整個行。

預期產出將是

student 50 
animals 20 

如果該值是零的所有行,我應該得到郵件提示,如「所有值都爲零」。

回答

1

爲什麼不使用Perl:

perl -ane 'print if($.!=1 && $F[1]!=0)' your_file 
+0

我只有Unix環境 – Vignesh

+0

Perl是標準Unix環境的一部分。 –

2

這可以工作:

$ awk '$2' file 
Table_name Value 
student 50 
animals 20 

如果二路字段不0則條件評估爲真 - 注意{print $0}awk默認塊,因此它可以,如果這是唯一的動作被忽略你想要表演。

關於郵件提醒,我認爲你最好展示一些你的代碼來提供一個引用點。

+0

它不是'can'。它應該是'will' – Vijay

+0

我沒有得到你。你的命令會給你下面的輸出0 50 0 20對嗎?我想要整行。 – Vignesh

+0

更具可讀性:'awk'$ 2> 0'文件' – Vijay

2

繼AWK應同時滿足您的要求:

awk 'NR>1 && $2 {nz=1;print}; END{if (!nz) print "all zeroes, send email"}' file 
  • 你只需要與你的郵件發送命令來替換print "all zeroes, send email"
+0

太棒了!非常感謝:) – Vignesh

1

很簡單使用awk,

awk -F " " '{if($2 > 0) print $0}' your_file 
+2

這很好,但請注意,您正在使用一些已在默認設置下的選項:'-F「」','print $ 0'。 – fedorqui

2

代碼GNU

 
$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 
相關問題