示例文件:
####################################################
序号       优先级       字段1        字段2
1              5                   abc            def
2              5                   ae3            wff
6              4                   lkdf           e3w
3              8                   w3ef          defe
####################################################
对于上述文件想实现这样的排序结果,先按优先级倒序排序,如果优先级相同,再按A(升序) D(降序)S(随机)排序
 
#代码如下:
case $sorttype in
  "A")
      sort -k2nr -k1n -o $recordout $recordin
      ;;
  "D")
      sort -k2nr -k1nr -o $recordout $recordin
      ;;
  "S")
      awk 'BEGIN {srand();OFS="\t"} {print $0,rand()*1000000}' $recordin |sort -k2nr -k5n|awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}' >$recordout
      ;;
  "*")
      :
      ;;
esac