您的位置:首页 > 运维架构 > Linux

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"

}

'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: