shell 及相关 / 未分类 · 2012年3月29日

按照人名的汉语拼音的顺序排序

#!/bin/ksh
  #scriptname:sortname
  #write by:bjgirl
  #date:2003/09/11
  name=$(
  for i in $(awk -F| '{print $2}' datafile)
  do
  echo -n "$i=";echo $i|od -c|awk '{print $2}'
  done|tr -s 'n'|sort -n -t"=" -k 2|awk -F= '{print $1}')
  for j in $name
  do
  sed -n /${j}/p datafile
  done

  [/code:1:11157874a8]
  结果
  [code:1:11157874a8]
  $sortname
  1231017911|黄伟|1231017911|2003/07/07|2004/07/03|200000.0|0.0|6.6375
  1231021548|黄伟希|1231021548|2001/04/18|2001/10/18|20000.0|10000.0|6.975
  1231021548|黄伟希|1231021548|2001/04/18|2001/10/18|20000.0|10000.0|6.975
  1231054277|李八|1231054277|2002/04/18|2002/12/18|50000.0|0.0|6.6375
  1231024763|李四|1231024763|2000/08/11|2001/08/11|10000.0|0.0|7.3125
  1231053510|孙七|1231053510|2002/02/08|2002/08/08|50000.0|0.0|6.975
  1231005010|苏义|1231005010|2003/06/25|2003/08/25|200000.0|0.0|6.3
  1231017864|苏义强|1231017864|2001/04/29|2001/08/30|10000.0|0.0|6.975
  1231030010|苏义红|1231030010|2001/12/27|2002/12/26|60000.0|38000.0|7.3125
  1231042038|苏义红|1231042038|2003/02/27|2003/12/20|40000.0|0.0|6.6375
  1231017864|苏义强|1231017864|2001/04/29|2001/08/30|10000.0|0.0|6.975
  1231030010|苏义红|1231030010|2001/12/27|2002/12/26|60000.0|38000.0|7.3125
  1231042038|苏义红|1231042038|2003/02/27|2003/12/20|40000.0|0.0|6.6375
  1231030010|苏义红|1231030010|2001/12/27|2002/12/26|60000.0|38000.0|7.3125
原理很简单:把汉字转换成ASCII码后再排序
from http://doc.linuxpk.com/3814.html