使用模式,结构化命令.sh Shell 脚本

Shell 片段 · codes/shell/查找替换文本/gawk/使用模式,结构化命令.sh · 59 行

源码

#!/usr/bin/env bash

#正则表达式

gawk 'BEGIN{FS=","}
/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

查看上游源码 ↗

内容与示例整理自 dunwu/linux-tutorial,遵循 CC BY-SA 4.0;本站保留原作者署名与许可,命令仅供学习,执行前请核对目标环境。