Use awk to do simple statistics job

Basic use

Example:

print requred fields

awk -F "/t" '$2>30{print $3"/t"$4}' a.txt > b.txt

This sentence means the seperator in a.txt is "/t" and print the third and forth fields when the second filed is greater than 30.

 

Use BEGIN and END

format:awk -F "/t" 'BEGIN{ initial variables} { if (condition) { compute body} } END{ output instruction}.

 

Example

print the sum of specified fields.

awk -F "/t" 'BEGIN{sum = 0} {if ($3/$4>0.5 && $4>40) { sum += $4} }END{print sum}' a.txt 

Above sentence print the sum of the forth fields when the third and forth fields meet the condition  ($3/$4>0.5 && $4>40)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章