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

本文档介绍了如何使用awk命令对文档中的特定字段进行排序。首先定义了问题,即如何利用awk处理数据并按特定列排序。接着,详细解析了awk代码的实现逻辑。最后,展示了代码运行后的实际结果,证明了awk命令的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值