Chinaunix

标题: 多字段(数字或字符串)排序的问题??? [打印本页]

作者: gnubuntu    时间: 2010-06-04 17:23
提示: 作者被禁止或删除 内容自动屏蔽
作者: ywlscpl    时间: 2010-06-04 17:41
月份一般都是缩写为3个字母
sort中有个选项可以直接对月份缩写排序

  1.        -M, --month-sort
  2.               compare (unknown) < ‘JAN’ < ... < ‘DEC’
复制代码
但你的数据估计月份是不缩写的 May June,这样只能针对这些月份手工指定一个排序规则
作者: gnubuntu    时间: 2010-06-04 17:47
提示: 作者被禁止或删除 内容自动屏蔽
作者: gnubuntu    时间: 2010-06-04 17:59
提示: 作者被禁止或删除 内容自动屏蔽
作者: lkk2003rty    时间: 2010-06-04 17:59
  1. sort -t "|" -k3 -k2 -M -k1 file
复制代码
试试。。。。。。。
作者: iori809    时间: 2010-06-04 18:01
我要下班没时间做了~给你个思路吧~ 我觉得可以转换成秒然后在排序~
我回家再想想
作者: gnubuntu    时间: 2010-06-04 18:03
提示: 作者被禁止或删除 内容自动屏蔽
作者: gnubuntu    时间: 2010-06-04 18:04
提示: 作者被禁止或删除 内容自动屏蔽
作者: ywlscpl    时间: 2010-06-04 18:24
回复 8# gnubuntu


  LANG=C sort -t '|' -k3,3 -k2,2M -k1,1  file
作者: gnubuntu    时间: 2010-06-04 18:25
提示: 作者被禁止或删除 内容自动屏蔽
作者: lkk2003rty    时间: 2010-06-04 19:11
回复 9# ywlscpl


    弱弱的问一下  为什么前面要有LANG=C 这个有什么作用?
作者: lkk2003rty    时间: 2010-06-04 19:19
回复 10# gnubuntu


    转成大写后用9楼的  俺错了。。。。sorry。。。。
作者: iori809    时间: 2010-06-05 10:41
awk -F"|" '{print $NF" "system("date -d "$1 $2 $3" +%s")}' linux.156 | sed 'N;s/\n/|/' | sort -n | awk -F"|" '{print strftime("%d|%b|%Y",$1)" "$2}'
12|May|2000 Birth Control Pill Sparked Contraceptive Revolution 0
28|May|2000 Bluegrass Jam Sessions Popular Across United States 0
01|Jun|2000 Egypt Opens Border With Gaza Strip to Allow Humanitarian Aid 0
24|May|2007 Arturo Sandoval Slows the Pace on 'A Time For Love' 0
19|May|2008 Sahel Food Security Depends on Reinvesting in Local Agriculture 0
19|May|2008 UN Looking for Ways to Keep Girls in School 0
24|May|2009 Most Powerful Woman in American History? 0
13|May|2010 Britain's New Cabinet Holds First Meeting to Tackle Deficit 0
13|May|2010 Niger Faces Severe Food Shortages 0
14|May|2010 Anti-Incumbent Winds Blowing in Advance of 2010 Elections 0
14|May|2010 Thai Protest Leaders Press for Ceasefire as Deadly Clashes Continue 0
15|May|2010 Gulf Oil Spill Presents New Challenges to Vietnamese Fishermen in L 0
19|May|2010 Study Urges US to Push Mideast Allies Toward Democracy 0
20|May|2010 Farming has Superstars, Too 0
25|May|2010 US Sees Progress in Economy Talks with China 0
27|May|2010 Oil Spill Sparks Concerns About Imported Seafood 0
28|May|2010 Flotilla Challenges Israeli Blockade of Gaza 0
28|May|2010 NATO Prepares to Test President Obama's 'Surge' Strategy in Afgh 0
28|May|2010 Studies Find Increasing Health Benefits From Vitamin D 0
01|Jun|2010 Ethiopian Opposition Coalition Calls for New Vote 0

写了一个很难看的~只能代表一种思路跟大家分享一下哈哈。效率太差了
作者: l_penny    时间: 2010-06-05 11:59
本帖最后由 l_penny 于 2010-06-05 12:01 编辑
  1. [root@server ~]# sort -t"|" -k3n -k2M -k1  urfile  
复制代码
12|May|2000|Birth Control Pill Sparked Contraceptive Revolution
28|May|2000|Bluegrass Jam Sessions Popular Across United States
01|June|2000|Egypt Opens Border With Gaza Strip to Allow Humanitarian Aid
24|May|2007|Arturo Sandoval Slows the Pace on 'A Time For Love'
19|May|2008|Sahel Food Security Depends on Reinvesting in Local Agriculture
19|May|2008|UN Looking for Ways to Keep Girls in School
24|May|2009|Most Powerful Woman in American History?
13|May|2010|Britain's New Cabinet Holds First Meeting to Tackle Deficit
13|May|2010|Niger Faces Severe Food Shortages
14|May|2010|Anti-Incumbent Winds Blowing in Advance of 2010 Elections
14|May|2010|Thai Protest Leaders Press for Ceasefire as Deadly Clashes Continue
15|May|2010|Gulf Oil Spill Presents New Challenges to Vietnamese Fishermen in L
19|May|2010|Study Urges US to Push Mideast Allies Toward Democracy
20|May|2010|Farming has Superstars, Too
25|May|2010|US Sees Progress in Economy Talks with China
27|May|2010|Oil Spill Sparks Concerns About Imported Seafood
28|May|2010|Flotilla Challenges Israeli Blockade of Gaza
28|May|2010|NATO Prepares to Test President Obama's 'Surge' Strategy in Afgh
28|May|2010|Studies Find Increasing Health Benefits From Vitamin D
01|June|2010|Ethiopian Opposition Coalition Calls for New Vote
作者: gnubuntu    时间: 2010-06-05 14:36
提示: 作者被禁止或删除 内容自动屏蔽
作者: gnubuntu    时间: 2010-06-05 14:38
提示: 作者被禁止或删除 内容自动屏蔽
作者: ywlscpl    时间: 2010-06-06 15:58
回复  ywlscpl


    开始的LANG=C 是什么?也不用 ; 隔开吗?这个是什么来的?


--month-sort 字段 ...
gnubuntu 发表于 2010-06-05 14:36



    "VAR=value command"这样的形式是将VAR作为一个环境变量传给命令command,但VAR=value对当前shell不产生影响

LANG是系统环境变量,用来设置系统语言。C代表英文环境,ASCII 编码
LANG的设置会对sort结果产生影响,LANG=C设置后sort按ASCII 编码排序
作者: heqingbluesky    时间: 2010-06-10 16:56
sort -t "|" -k3,3 -k2,2M -k1,1 testfile.txt




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2