使用awk对文档中特定字段的排序
来源:互联网 发布:乐知英语在哪里 编辑:程序博客网 时间:2024/04/30 12:18
http://blog.csdn.net/amuseme_lu/article/details/6985434
使用awk对文档中特定字段的排序
----------------------------------------------------1. 问题定义
现在要对如下文档按特定字段排序,- lemo@debian:~/Testspace/awk$ cat file
- Name Sex Salary
- Lemo man 4000
- Jok woman 3000
- Job man 6000
- Petty man 9000
我现在想对其按Salary从高到低进行排序,这时我们就会想到用数据库进行排序,这样的话要把这些数据导入数据库,如Mysql, sqlite等,但当我们手头没有这些数据库时,其实我们可以用一个叫做awk的程序设计语言来处理,它的优点是非常适合处理结构化数据。
2. 代码分析
- # @auth lemo.lu
- # @data 2011.11.16
- # use to sort the record with specific field
- # BEGIN BLOCK
- BEGIN{
- print "BEGIN SORT"
- }
- ##############################################
- # NR - number of records , start from 1
- # NF - number of fields
- # $0 - the whole line
- # $1,$2 - the first column as $1, the second column as $2
- ##############################################
- NR==1 { # if number of record equals to 1
- for (i=1; i<=NF; i++) {
- if ( $i == fldName ) {
- fldNr = i
- }
- }
- head = $0 # store field names that is store in first line
- next # like continue
- }
- # 32 - width => the result is padded with black spaces if the value to be printed
- # is shorter than this number, There , if the length of sorted value is larger that
- # 32, the result will be wrong
- { values[NR] = sprintf("%32s%s", $fldNr, $0) }
- # END BLOCK
- END {
- n = asort(values) # sorts the content of values and return the number of elments in the array source, the default order is ascending.)
- print head
- if( "des" == order)
- {
- for (i=n; i>=1; i--) {
- print substr(values[i],33) # remove the sort values
- }
- }
- else{
- for (i=1; i<=n; i++) {
- print substr(values[i],33) # remove the sort values
- }
- }
- }
我们可以把这代程序放在tst.awk文件中,以gawk命令调用之。
3. 运行结果
下面是一个升序的结果- lemo@debian:~/Testspace/awk$ gawk -v fldName="Salary" -f tst.awk file
- BEGIN SORT
- Name Sex Salary
- Jok woman 3000
- Lemo man 4000
- Job man 6000
- Petty man 9000
下面是一个降序的结果
- emo@debian:~/Testspace/awk$ gawk -v fldName="Salary" -v order="des" -f tst.awk file
- BEGIN SORT
- Name Sex Salary
- Petty man 9000
- Job man 6000
- Lemo man 4000
- Jok woman 3000
呵呵,是不是很简单啊。一般来说,awk会结合sed来用,sed是一个字符流编辑器,可以很好的完成对多个文件的一系列编辑工作。
4. 参考
http://www.gnu.org/s/gawk/manual/html_node/String-Functions.html#String-Functions
0 0
- 使用awk对文档中特定字段的排序
- 使用awk对文档中特定字段的排序
- 使用awk对文档中特定字段的排序
- iPhone应用程序中对数组的特定字段进行排序
- 如何对dbgrideh中特定的字段求和或者合计
- 对列表以可计算的(特定的)字段来排序
- sql按照特定的字段排序
- awk加Sort实现对前N-1个字段作Key,最后一个字段作Value的排序
- java对集合元素的特定排序
- AWK 使用循环取出所有的字段
- MySQL中order by语句对null字段的排序
- MySQL中order by语句对null字段的排序
- MySQL中order by语句对null字段的排序
- 对jsonarray中某个对象的某一个字段进行排序
- js对数组中对象字段的排序
- 使用UTF-8的字段对中文排序
- 只导入mysqldump文件中特定的表 -awk效率
- 使用google搜索特定类型的文档
- xib文件关联问题
- 第四章 4.1,4.2
- (LeetCode)Path Sum空指针异常问题
- HEVC代码追踪(六):compressCU
- POJ 3687 Labeling Balls(逆向拓扑)
- 使用awk对文档中特定字段的排序
- 牛人博客和主页---站得高,看得更远(企业大牛的博客)
- hdu 2639 Bone Collector II
- Linux常用系统变量:
- 设计一个结构体bign来存贮高精度非负整数
- 号称是世界上所有的Java程序员都会犯的错误
- 第13周项目5-完成类似字符串复制的功能
- windows开发环境下 Apache+PHP环境整合安装
- CodeBlocks安装方法(Nightly Build版) 和几款高亮皮肤