LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 882|回复: 5

先awk再sort与先sort再awk,得出不同的结果。

[复制链接]
发表于 2005-10-10 22:57:01 | 显示全部楼层 |阅读模式
先awk再sort与先sort再awk,得出不同的结果。
平台RHEL4-U1,bash-3.0-19.2,gawk-3.1.3-10.1,sort-5.2.1
我先后执行了两条命令

awk -F: '{print $1,$2,$3}' /etc/passwd |sort +2 > as-passwd

sort -t: +2 /etc/passwd |awk -F: '{print $1,$2,$3}' > sa-passwd

当我对比时候发现两条命令执行的最终结果不一样(请留意输出结果的前四行),请问为什么呢?

附上as-passwd的输出结果
root x 0
bin x 1
uucp x 10
operator x 11

games x 12
gopher x 13
ftp x 14
daemon x 2
nscd x 28
rpcuser x 29
adm x 3
rpc x 32
netdump x 34
rpm x 37
lp x 4
mailnull x 47
sync x 5
smmsp x 51
shutdown x 6
nfsnobody x 65534
haldaemon x 68
vcsa x 69
halt x 7
sshd x 74
pcap x 77
mail x 8
dbus x 81
news x 9
nobody x 99

附上sa-passwd的输出结果
root x 0
uucp x 10
operator x 11
bin x 1

games x 12
gopher x 13
ftp x 14
daemon x 2
nscd x 28
rpcuser x 29
rpc x 32
netdump x 34
adm x 3
rpm x 37
mailnull x 47
lp x 4
sync x 5
smmsp x 51
shutdown x 6
nfsnobody x 65534
haldaemon x 68
vcsa x 69
halt x 7
sshd x 74
pcap x 77
mail x 8
dbus x 81
news x 9
nobody x 99
发表于 2005-10-11 01:49:53 | 显示全部楼层
Better use
  1. sort -t: -k3,3n
复制代码
without specifying sorting with numeric value, it outputs funny stuff sometimes..
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-12 22:11:38 | 显示全部楼层
Post by yongjian
Better use
  1. sort -t: -k3,3n
复制代码
without specifying sorting with numeric value, it outputs funny stuff sometimes..

为何一定要明确数字域?难道这软件有这么的不确定性。
回复 支持 反对

使用道具 举报

发表于 2005-10-12 22:25:21 | 显示全部楼层
Not because of the domain but the number. Sort by default is not using the numeric value.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-12 23:45:03 | 显示全部楼层
Post by yongjian
Not because of the domain but the number. Sort by default is not using the numeric value.

刚才按照版主的提示上机器做实验,在只用sort的情况下,把第三域作为数字的形式排序,是得出想要的结果,但这种形式不能用在混合了awk和sort的情景下,象我举的例子,请版主尝试一下用我的例子上机器来实验一下。
又或者说我们不一定把第三个域当做数字,而把它当作是sort默认的字符模式时,那还是没有解决我所提出的问题。
回复 支持 反对

使用道具 举报

发表于 2005-10-13 02:59:17 | 显示全部楼层
Post by on-fire
刚才按照版主的提示上机器做实验,在只用sort的情况下,把第三域作为数字的形式排序,是得出想要的结果,但这种形式不能用在混合了awk和sort的情景下,象我举的例子,请版主尝试一下用我的例子上机器来实验一下。
又或者说我们不一定把第三个域当做数字,而把它当作是sort默认的字符模式时,那还是没有解决我所提出的问题。

They should be the same. Note: you need to be consistent on your commands.

  1. awk -F: '{print $1,$2,$3}' /etc/passwd |sort +2n >as-passwd
  2. sort -t: +2n /etc/passwd |awk -F: '{print $1,$2,$3}' > sa-passwd
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表