awk

xiaoxiao2022-06-11  44

awk认知

awk 执行模式

所需文件: 1.pattern { action }

$3 == 0 { print $1 } //awk '$3 >0 { print $1, $2 * $3 }' emp.data //awk '$3 == 0 { print $1 }' file1 file2

基础输出print,字段数量NF,行号NR

{ print "total pay for", $1, "is", $2 * $3 } // awk '{ print NR,"name ",$1,"salary ",$2*$3,"字段数量",NF}' emp.data

逻辑运算

NF != 3 { print $0, "number of fields is not equal to 3" } $2 < 3.35 { print $0, "rate is below minimum wage" } $2 > 10 { print $0, "rate exceeds $10 per hour" } $3 < 0 { print $0, "negative hours worked" } $3 > 60 { print $0, "too many hours worked" }

BEGIN与END

awk 'BEGIN{print "Name","path","hours"} { print $1,$2,$3} END{ {print "total is",pay}{print "average is",pay/NR}}' emp.data

变量应用

awk '{ nc = nc + length($0) + 1 nw = nw + NF } END { print NR, "lines,", nw, "words,", nc, "characters" }'

ifelse while for array

awk ' { nc = nc + length($0) + 1 nw = nw + NF arr[1]="hello the world"; } { for(i=1;i<5;i++) { {print "now i is" ,i} { if(i>3){print "my name is zylg"} else {print "hello zylg"} } while(1) { {print "exit"} break; } { print arr[1] } } } END { print NR, "lines,", nw, "words,", nc, "characters" }' ~/removesoft
转载请注明原文地址: https://www.6miu.com/read-4931870.html

最新回复(0)