Linux之awk学习
2012-07-10 22:27
176 查看
#这个实例计算在命令行上指定的某个文件中的每一列数字的总和。
if (test $# = 0)then
echo "you must apply a number."
exit 1
fi
cat $1 | awk ' NR == 1 # awk < $1
{
nfields = NF #set nfields to number of fields in the record (NF)
}
{
if($0 ~ /[^0-9. \t]/) #check each record to see if it contains any characters are not numbers.
{ #periods.spaces.or tabs
printf "it is not a number record\t " $0
printf "\n"
}
else
{
for(count = 1;count <= nfields;count ++) #for good records loop
{
printf "%10.2f",$count > "out"
sum[count] += $count
gtotal += $count
}
print "\n" > "out"
}
}
END {
for(count = 1;count <= nfields;count++)
{
printf " -------" > "out"
}
print "\n" > "out"
for(count = 1;count <= nfields;count++)
{
printf"%10.2f",sum[count] > "out"
}
print "\n\n Grand Total" gtotal "\n" > "out"
}
'
相关文章推荐
- linux学习之linux百问4,awk了解
- Linux学习笔记---day8(3.27)sed,awk与正则表达式
- linux awk 学习笔记
- Linux命令学习:awk
- Linux学习 - awk使用
- Linux学习笔记_基本命令复习(seq sed awk)
- linux运维实战练习及linux shell脚本、awk、sed工具命令学习总结
- linux学习之sed、awk
- linux学习第二十六篇:正则介绍,grep,sed,awk命令
- linux学习笔记(18) - sed和awk
- linux命令学习(1)-awk
- Linux学习之awk命令
- Linux学习---shell编程(08-awk)
- Linux-awk学习
- Linux-awk学习笔记
- Linux命令学习:统计socket各种状态的连接数(sed/awk)
- 轻松学习Linux之AWK使用初步
- linux awk 学习笔记三(结构化命令、函数)
- Linux学习笔记------day9(3.28)-----find中exec的用法,awk赋值
- Linux命令之awk学习笔记