linux按照指定列对文件排序



linux按照指定列对文件排序

(2010-02-25 10:33:17)

参考:http://www.justwinit.cn/post/3908/

 

sort如何按指定的列排序

0000            27189           41925425
065f            15              41925425
0663                          41925425
0675                          41925425
0691            76              41925425
0693                          41925425
06a7            82              41925425
06a9            58              41925425
06bf            68              41925425
06c1            56              41925425
06d7            145             41925425
06e1            127             41925425
06e3            230             41925425
06e5                          41925425
06ef            48              41925425
06f1            463             41925425
06ff            3847            41925425
070b            2376            41925425
070d            3408            41925425
0713            109025          41925425
0715                          41925425
0726            134             41925425
我想按第二列值进行排序,从大到小的顺序
看了下man,还是不大明白,还请大家指点一二。

 

答案:sort -n -k2 file.txt 或则sort -g -k2 file.txt(从小到大排序)

-n, --numeric-sort         

 -g, --general-numeric-sort 

 sort -rn -k2 all_contig.txt (加-r从大到小排序)

 


用法:sort [选项]... [文件]...
Write sorted concatenation of all FILE(s) to standard output.

长选项必须用的参数在使用短选项时也是必须的。
顺序选项:

  -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     显示此帮助信息并退出
      --version  输出版本信息并退出

POS is F[.C][OPTS], where F is the field number and C the character position
in the field.  OPTS is one or more single-letter ordering options, which
override global ordering options for that key.  If no key is given, use the
entire line as the key.

SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is -, read standard input.

*** WARNING ***
The locale specified by the environment affects sort order.
Set LC_ALL=C to get the traditional sort order that uses
native byte values.

请向 <bug-coreutils@gnu.org> 报告错误。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用sort命令对文件内容进行排序。 sort命令的基本语法为: sort [选项] 文件名 常用选项包括: -n:按数值大小排序,而不是按字典序排序。 -r:倒序排序。 -k n:指定按第n个字段排序。 例如,对文件data.txt按第一列数值大小进行排序,可以使用以下命令: sort -n -k 1 data.txt 对文件data.txt按第二列字典序倒序排序,可以使用以下命令: sort -r -k 2 data.txt ### 回答2: 在Linux中,可以使用多种命令和工具对文件内容进行排序。 1. sort命令:这是最常用的命令之一,用来对文件行进行排序。它可以按照字母顺序或者数字大小进行排序。例如,使用`sort filename.txt`命令可以按行对文件内容进行字母顺序排序。可以通过多种选项来控制排序的方式,如`-n`选项可以按照数字大小排序。 2. uniq命令:该命令用于去重操作,但也可以与sort命令结合使用来对文件内容进行排序。例如,使用`sort filename.txt | uniq`命令可以对文件内容进行去重并排序。 3. awk命令:awk是一种强大的文本处理工具,在其中可以使用sort函数对文件内容进行排序。使用类似`awk '{print $0 | "sort"}' filename.txt`的命令可以对文件内容进行排序。 4. sed命令:sed是一种流编辑器,可以对文件进行处理。可以使用sort命令与sed相结合来进行排序操作。例如,使用`sed '1,$=;s/$/\t&/' filename.txt | sort -n | sed 's/.*\t//'`命令可以对文件内容进行数字排序。 总之,Linux提供了多种命令和工具来对文件内容进行排序操作,可以根据不同的需求选择合适的方法来进行排序。以上仅是其中几个常用方法,还有其他更复杂的排序方式可以进行探索。 ### 回答3: 在Linux系统中对文件内容进行排序的方法有很多种,下面我来介绍几种常用的方法。 1. 使用sort命令 sort命令可以对文本文件按照行进行排序,默认按照字母顺序逐行排序。可以使用不同的选项来实现更复杂的排序方式。例如,使用-n选项可以按照数值大小进行排序,使用-r选项可以倒序排列。 2. 使用uniq命令 uniq命令可以将相邻的重复行合并为一行,并且可以根据需要进行排序。可以通过对文件进行排序后再使用uniq命令,来对文件内容进行排序去重操作。 3. 使用awk命令 awk命令是一个强大的文本处理工具,可以用于对文件内容进行各种操作,包括排序。通过使用awk命令的内置排序函数,可以对文件内容进行排序。 4. 使用sed命令 sed命令是另一个文本处理工具,用于对文件内容进行替换、删除、插入等操作。通过调用sed命令来实现对文件内容的排序。 5. 使用Python脚本 在Linux系统中,可以使用Python编写脚本来完成对文件内容的排序操作。可以使用Python的内置函数sorted()对文件进行排序,并输出排序结果。 总而言之,Linux系统提供了各种命令和工具来实现对文件内容的排序操作,可以根据具体需求选择不同的方法来完成排序。以上是其中几种比较常见的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值