The conditional statements in awk were borrowed from the C language. They are used to control the flow of the program in making decisions.
awk 条件语句本身不难,需要熟练掌握其用法和格式,下面通过几个示例学习awk的条件语句
1、FORMAT
if (expression) {
statement; statement; ...
}
example
1 % awk '{if ( $6 > 50 ) print $1 "Too high"}' filename
2 % awk '{if ($6 > 20 && $6 <= 50){safe++; print "OK"}}' filename
2、if/else Statements
{if (expression) {
statement; statement; ...
}
else{
statement; statement; ...
}
}
example
1 % nawk '{if( $6 > 50) print $1 " Too high" ;\
else print "Range is OK"}' filename
2 % nawk '{if ( $6 > 50 ) { count++; print $3 } \
else { x+5; print $2 } }' filename
总结:掌握格式,多多的去写去练啦