如何使用shell脚本进行排序

时间:2014-07-16 09:16:09

标签: shell sorting unix ubuntu

        clear
echo "==========================================="
echo "  Top 5 Best Seller"
echo "-------------------------------------------"
echo ""

printf "${UL}$(tput bold)%-40s\t%-20s\t%-10s\t%-20s\t%-5s\t%-10s$(tput sgr0)\n\n" "Title" "Author" "Price" "Qty Available" "Sold" "Total Sales"

IFS=:
while read FIRST SECOND THIRD FOURTH FIFTH
do
    SALES=$(echo $THIRD*$FIFTH | bc)
    printf "%-40s\t%-20s\t%-10s\t%-20s\t%-5s\t%-10s\n" "$FIRST" "$SECOND" "\$$THIRD" "$FOURTH" "$FIFTH" "\$$SALES"
done < BookDB.txt | sort -t: -n -k5 -r BookDB.txt

当我的文件看起来像这样:

All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101

我似乎不能像这样安排它 输出应该是这样的事情:

Title                                       Author                  Price       Qty Available           Sold    Total Sales
All About Ubuntu                            Ubuntu Team             $76.00      55                      133     $10108.00 
Happy Day                                   Mary Ann                $12.99      197                     101     $1311.99         
Catch Me If You Can                         Mary Ann                $23.60      6                       2       $47.20    

0 个答案:

没有答案