linux - linux中的排序命令不能“正确”工作
问题描述
我在 Linux 上工作,sort
命令返回不符合预期。
输入文本:
$ cat input.txt
rep1_1.fq
rep1_2.fq
rep12_1.fq
rep12_2.fq
命令和输出:
$ sort input.txt
rep1_1.fq
rep12_1.fq
rep12_2.fq
rep1_2.fq
$ sort --version
sort (GNU coreutils) 8.28
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
排序后,我预计rep1_2.fq
会在 之后rep1_1.fq
,但结果不同。
解决了
根据@Federico klez Culloca 的建议,使用LC_ALL=C
$ LC_ALL=C sort input.txt
rep12_1.fq
rep12_2.fq
rep1_1.fq
rep1_2.fq
已编辑
使用LC_ALL=C
还修复目录中的文件排序。
如果当前目录中有四个文件:
$ LC_ALL= ls
rep1_1.fq rep12_1.fq rep12_2.fq rep1_2.fq
$ LC_ALL=C ls
rep12_1.fq rep12_2.fq rep1_1.fq rep1_2.fq
解决方案
尝试使用version-sort
. 从手册:
-V, --version-sort
natural sort of (version) numbers within text
这是使用您的示例的输出:
$ sort -V input.txt
rep1_1.fq
rep1_2.fq
rep12_1.fq
rep12_2.fq
推荐阅读
- html - 在角度模板中增加日期
- reactjs - React hooks 组件重载
- php - 如何在模型 laravel 中访问三关系
- c# - SharePoint CSOM 查询返回每个字段 null
- python - ModuleNotFoundError:没有名为“main”的模块,但路径似乎正确
- bash - Bash 语法帮助 - mapfile/readarray(将文本拆分为数组)
- html - 悬停时带下划线的菜单项跳转
- c++ - 带有未初始化布尔值的三元组导致 macOS 上的缓冲区溢出
- python - 带有 csv 文件的字典没有读取每一列
- javascript - 有没有办法在单个 js 文件中为特定页面 URL 调用特定的 javascript 函数?