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)
本文介绍了AWK命令的基本使用方法及其高级特性,包括如何利用BEGIN和END块进行变量初始化和输出指令。通过实例展示了如何筛选特定字段及计算条件下的字段总和。
6972

被折叠的 条评论
为什么被折叠?



