linux-tutorial/codes/shell/查找替换文本/gawk/使用模式,结构化命令.sh

59 lines
494 B
Bash
Raw Permalink Normal View History

2019-10-29 18:22:19 +08:00
#!/usr/bin/env bash
2019-10-10 08:56:31 +08:00
2019-05-10 11:17:57 +08:00
#正则表达式
2019-10-10 08:56:31 +08:00
gawk 'BEGIN{FS=","}
2019-05-10 11:17:57 +08:00
/11/{print $1}
' test
#if-else语句
gawk '{
if($1 > 20)
{
x=$1*20
print x
}
else
{
x=$1/2
print x
}
}' test
#while 语句
gawk '{
total = 0
i=1
while(i<4)
{
total+=$i
i++
}
avg = total/3
print "Average:".avg
}' test
#do-while语句
gawk '{
total=0
i=1
do
{
total += $i
i++
}while(total < 150)
print total }' test
#for语句
gawk '{
total = 0
for (i=1; i<4; i++)
{
total+=$i
}
avg = total/3
print "Average:".avg
}' test