使用awk对文档中特定字段的排序

使用awk对文档中特定字段的排序
----------------------------------------------------

1. 问题定义

现在要对如下文档按特定字段排序,
[html]  view plain  copy
  1. lemo@debian:~/Testspace/awk$ cat file  
  2. Name    Sex Salary   
  3. Lemo    man 4000   
  4. Jok woman   3000   
  5. Job man 6000   
  6. Petty   man 9000  

我现在想对其按Salary从高到低进行排序,这时我们就会想到用数据库进行排序,这样的话要把这些数据导入数据库,如Mysql, sqlite等,但当我们手头没有这些数据库时,其实我们可以用一个叫做awk的程序设计语言来处理,它的优点是非常适合处理结构化数据。


2. 代码分析

[html]  view plain  copy
  1. # @auth lemo.lu  
  2. # @data 2011.11.16  
  3. # use to sort the record with specific field  
  4.   
  5. # BEGIN BLOCK  
  6. BEGIN{  
  7.     print "BEGIN SORT"  
  8. }  
  9.   
  10.   
  11. ##############################################  
  12. # NR - number of records , start from 1  
  13. # NF - number of fields  
  14. # $0 - the whole line   
  15. # $1,$2 - the first column as $1, the second column as $2  
  16. ##############################################  
  17.   
  18.   
  19. NR==1 {  # if number of record equals to 1  
  20.     for (i=1; i<=NF; i++) {   
  21.     if ( $i == fldName ) {   
  22.         fldNr = i   
  23.     }   
  24.     }   
  25.     head = $0  # store field names that is store in first line  
  26.     next       # like continue  
  27. }   
  28.   
  29.   
  30. # 32 - width => the result is padded with black spaces if the value to be printed  
  31. # is shorter than this number, There , if the length of sorted value is larger that   
  32. # 32, the result will be wrong  
  33. { values[NR] = sprintf("%32s%s", $fldNr, $0) }   
  34.   
  35.   
  36. # END BLOCK  
  37. END {   
  38.     n = asort(values) # sorts the content of values and return the number of elments in the array source, the default order is ascending.)  
  39.     print head   
  40.     if( "des" == order)  
  41.     {  
  42.     for (i=n; i>=1; i--) {   
  43.         print substr(values[i],33)  # remove the sort values  
  44.     }   
  45.     }  
  46.     else{  
  47.     for (i=1; i<=n; i++) {   
  48.         print substr(values[i],33)  # remove the sort values  
  49.     }   
  50.     }  
  51. }   

我们可以把这代程序放在tst.awk文件中,以gawk命令调用之。

3. 运行结果

下面是一个升序的结果
[html]  view plain  copy
  1. lemo@debian:~/Testspace/awk$ gawk -v fldName="Salary" -f tst.awk file  
  2. BEGIN SORT  
  3. Name    Sex Salary   
  4. Jok woman   3000   
  5. Lemo    man 4000   
  6. Job man 6000   
  7. Petty   man 9000  


下面是一个降序的结果

[html]  view plain  copy
  1. emo@debian:~/Testspace/awk$ gawk -v fldName="Salary" -v order="des" -f tst.awk file  
  2. BEGIN SORT  
  3. Name    Sex Salary   
  4. Petty   man 9000  
  5. Job man 6000   
  6. Lemo    man 4000   
  7. Jok woman   3000   


呵呵,是不是很简单啊。一般来说,awk会结合sed来用,sed是一个字符流编辑器,可以很好的完成对多个文件的一系列编辑工作。


4. 参考

http://www.gnu.org/s/gawk/manual/html_node/String-Functions.html#String-Functions



http://blog.csdn.net/amuseme_lu/article/details/6985434

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值