首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按id对unix文件进行排序

按id对unix文件进行排序
EN

Stack Overflow用户
提问于 2012-11-19 21:06:56
回答 3查看 2K关注 0票数 2

我想按id列对unix文件进行排序,但是当我使用排序、-k4,4或-k4,4n时,我得不到预期的结果。

感兴趣的列应该按如下方式排序:

代码语言:javascript
复制
id1
id2
id3
id4
etc.

相反,当我对-k4,4进行排序时,它是这样排序的

代码语言:javascript
复制
id1
id10
id100
id1000
id10000
id10001
etc.

我的unix版本使用以下排序函数:

代码语言:javascript
复制
sort --help
Usage: sort [OPTION]... [FILE]...
Write sorted concatenation of all FILE(s) to standard output.

Mandatory arguments to long options are mandatory for short options too.
Ordering options:

  -b, --ignore-leading-blanks  ignore leading blanks
  -d, --dictionary-order      consider only blanks and alphanumeric characters
  -f, --ignore-case           fold lower case to upper case characters
  -g, --general-numeric-sort  compare according to general numerical value
  -i, --ignore-nonprinting    consider only printable characters
  -M, --month-sort            compare (unknown) < `JAN' < ... < `DEC'
  -n, --numeric-sort          compare according to string numerical value
  -r, --reverse               reverse the result of comparisons

Other options:

  -c, --check               check whether input is sorted; do not sort
  -k, --key=POS1[,POS2]     start a key at POS1, end it at POS2 (origin 1)
  -m, --merge               merge already sorted files; do not sort
  -o, --output=FILE         write result to FILE instead of standard output
  -s, --stable              stabilize sort by disabling last-resort comparison
  -S, --buffer-size=SIZE    use SIZE for main memory buffer
  -t, --field-separator=SEP  use SEP instead of non-blank to blank transition
  -T, --temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or /tmp;
                              multiple options specify multiple directories
  -u, --unique              with -c, check for strict ordering;
                              without -c, output only the first of an equal run
  -z, --zero-terminated     end lines with 0 byte, not newline
      --help     display this help and exit
      --version  output version information and exit
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-19 21:10:41

使用-V--version-sort选项进行版本排序

sort -V -k4,4 file.txt

示例:

代码语言:javascript
复制
$ cat file.txt
id5
id3
id100
id1
id10

输出:

代码语言:javascript
复制
$ sort -V file.txt
id1
id3
id5
id10
id100

编辑:

代码语言:javascript
复制
sed -E 's/id([0-9]+)/\1/' file.txt | sort -n -k4,4 | sed -E 's/( *)([0-9]+)( *|$)/\1id\2\3/'

注意:此解决方案依赖于数据,只有在ID列之前找不到包含纯数字的列时才有效。

票数 2
EN

Stack Overflow用户

发布于 2012-11-19 21:41:15

作为sudo_o has already mentioned,最简单的方法是使用--version-sort,它对文本中出现的数字进行自然排序。

如果您的sort版本没有这个选项,那么解决这个问题的一种简单方法是在排序之前临时删除"id“前缀,然后替换它们。以下是使用awk的一种方法:

代码语言:javascript
复制
awk 'sub("^id", "", $4)' file.txt | sort -k4,4n | awk 'sub("^", "id", $4)'
票数 2
EN

Stack Overflow用户

发布于 2012-11-20 00:42:15

如果您的sort支持它,您还可以使用语法F.C来使用字段中的特定字符。

这将根据字段4排序,从字符3到10,数值:

代码语言:javascript
复制
sort -bn -k 4.3,4.10 file

这将在字段4上排序,从字符3到字段末尾,数值:

代码语言:javascript
复制
sort -bn -k 4.3,4 file
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13454464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档