全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 529|回复: 7

[疑问] 请教unix sort排序问题

[复制链接]
发表于 2022-11-7 22:58:57 | 显示全部楼层 |阅读模式
本帖最后由 ttyang 于 2022-11-7 23:02 编辑
  1. 102 Coriander Pl, Edgewater, AL 10101
  2. 222 North 7th St, Lanham, MO 34343
  3. 456 Skylaeerk Ln, Bowie, AX 23232
  4. 200 Stylaeerk Dr, Buity, IL 52369
复制代码


数据格式如上,就是美国地址.我这里用的假地址哈

要求按照最后一列的数字排序(正序)

格式特点:
1\ 每行都是逗号分隔成3列
2\ 第3列都是前面2个字符+空格+数字
3\ 第1列的空格数量是变化的,所以 -t " "无法实现

难点:
逗号分隔后,无法sort数字
空格分隔的话,数字所在的列是变化的

数字所在位置是:
第2个逗号后面的第2个空格后面
数字后面,其实还有字符,这里省略了
发表于 2022-11-8 15:32:17 | 显示全部楼层
sort target -t ',' -k 3.5
 楼主| 发表于 2022-11-8 17:52:33 | 显示全部楼层
l852018751 发表于 2022-11-8 15:32
sort target -t ',' -k 3.5

神奇,果然可以

请教大佬 3.5的实际意义,十分感谢
发表于 2022-11-8 17:59:06 | 显示全部楼层
但凡你man一下都不会发这个帖子,3列第5字符
 楼主| 发表于 2022-11-8 18:10:14 | 显示全部楼层
本帖最后由 ttyang 于 2022-11-8 18:15 编辑
HOH 发表于 2022-11-8 17:59
但凡你man一下都不会发这个帖子,3列第5字符


谢谢,水王

你的linux技术很厉害,见识过的

我100行代码,
你10行就搞定

这个man,对我真的没用
  1.        -k, --key=KEYDEF
  2.               sort via a key; KEYDEF gives location and type
复制代码


谷歌半天都没找到

顺便备注下,再次感谢两位大佬
#https://unix.stackexchange.com/questions/435492/force-gnu-sort-command-to-consider-only-the-first-n-decimal-points
发表于 2022-11-8 18:27:32 | 显示全部楼层
ttyang 发表于 2022-11-8 18:10
谢谢,水王

你的linux技术很厉害,见识过的


你这不叫man好吧,这个才叫man

       Sort keys can be specified using the options:

       -k keydef The keydef argument is a restricted sort key field  definition.  The  format  of
                 this definition is:

                     field_start[type][,field_end[type]]

                 where  field_start  and  field_end define a key field restricted to a portion of
                 the line (see the EXTENDED  DESCRIPTION  section),  and  type  is  one  or  more
                 modifiers  from  the  list  of characters 'b', 'd', 'f', 'i', 'n', 'r'.  The 'b'
                 modifier shall  behave  like  the  -b  option,  but  shall  apply  only  to  the
                 field_start  or  field_end  to  which  it is attached. The other modifiers shall
                 behave like the corresponding options, but shall apply only to the key field  to
                 which  they  are  attached;  they  shall  have  this  effect  if  specified with
                 field_start, field_end, or both. If any modifier is attached to a field_start or
                 to  a  field_end, no option shall apply to either. Implementations shall support
                 at least nine occurrences of the  -k  option,  which  shall  be  significant  in
                 command  line  order.  If  no  -k option is specified, a default sort key of the
                 entire line shall be used.

                 When there are multiple key fields, later keys shall be compared only after  all
                 earlier  keys  compare equal. Except when the -u option is specified, lines that
                 otherwise compare equal shall be ordered as if none of the options -d,  -f,  -i,
                 -n,  or  -k  were present (but with -r still in effect, if it was specified) and
                 with all bytes in the lines significant to the comparison. The  order  in  which
                 lines that still compare equal are written is unspecified.
 楼主| 发表于 2022-11-8 18:38:13 | 显示全部楼层
HOH 发表于 2022-11-8 18:27
你这不叫man好吧,这个才叫man


啊,咋这么欺负人,我的Ubuntu 20.04.4 LTS
运行
man sort
只有一点点,和--help一样

你是打开/usr/share/man/man1/sort.1.gz这个去阅读吗?

这得多麻烦呀

再次请教水王大佬
发表于 2022-11-8 18:47:07 | 显示全部楼层
ttyang 发表于 2022-11-8 18:38
啊,咋这么欺负人,我的Ubuntu 20.04.4 LTS
运行
man sort


https://manpages.ubuntu.com/manpages/jammy/en/man1/sort.1posix.html

你要实在不会找就用在线版的啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-5-7 12:47 , Processed in 0.088487 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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