我想要一份显示工资范围的报告。我已经尝试了下面的代码,但它不断计算文本文件中的所有数据,而不先搜索范围 请告知可以改进的地方。
TXT
3,Frank,CFO,91111453,Salaried,2333
1,Mary,CEO,93424222,Salaried,1111
5,John,Sales user,9321312,Commission,9999
7,Chris,Admin,98888753,Hourly,122
结果[我希望输出的内容]
Range total number
1-1000 1
1001-2000 2
代码:
echo "range Total"
awk '{t==$6}END{ if (t < 1001) count++ } END { print "$0 - 999\t\t"count }' $file
awk '{t==$6}END{ if (1001<$6<2000) count++ } END { print "$1000 - 2999\t\t"count }' $file
答案 0 :(得分:1)
试试这个awk程序
BEGIN {
FS=","
OFS="\t"
print "Range ", "total number"
}
{
ranges[int($6 / 1000)*1000]++
}
END {
for (r in ranges)
print r "-" (r+999) ":", ranges[r]
}
答案 1 :(得分:0)
Pure bash:
#!/bin/bash
while read LINE
do
LINE=${LINE##*,}
[[ ${LINE} -ge 1 ]] && [[ ${LINE} -le 1000 ]] && (( ++RANGE1 ))
[[ ${LINE} -ge 1001 ]] && [[ ${LINE} -le 2000 ]] && (( ++RANGE2 ))
done < "/path/to/report"
echo "
Range total number
1 - 1000 ${RANGE1}
1001 - 2000 ${RANGE2}
"
答案 2 :(得分:0)
也许你想拥有任意数量的范围而不是只有两个(纯粹的bash):
#!/bin/bash
#calculating
while read LINE
do
LINE=${LINE##*,} ; (( ++RANGE[LINE /= 1000] ))
done < "/your/file/here"
#printing
echo -e "Range\t\ttotal number"
for NR in {0..10}
do
echo -e "${NR}001 - $(( $NR + 1 ))000\t${RANGE[$NR]}"
done