Bash 并按顺序排序文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1747047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:41:54  来源:igfitidea点击:

Bash and sort files in order

bashsorting

提问by Open the way

with a previous bash script I created a list of files:

使用以前的 bash 脚本,我创建了一个文件列表:

data_1_box
data_2_box
...
data_10_box
...
data_99_box

the thing is that now I need to concatenate them, so I tried

问题是现在我需要连接它们,所以我尝试了

ls -l data_*

ls -l 数据_*

but I get

但我明白了

.....
data_89_box
data_8_box
data_90_box
...
data_99_box
data_9_box

but I need to get in the sucession 1, 2, 3, 4, .. 9, ..., 89, 90, 91, ..., 99

但我需要参加 1, 2, 3, 4, .. 9, ..., 89, 90, 91, ..., 99

Can it be done in bash?

可以在bash中完成吗?

回答by Puppe

ls data_* | sort -n -t _ -k 2

-n: sorts numerically
-t: field separator '_'
-k: sort on second field, in your case the numbers after the first '_'

-n:按数字排序
-t:字段分隔符 '_'
-k:按第二个字段排序,在您的情况下是第一个 '_' 之后的数字

回答by P?r Wieslander

How about using the -vflag to ls? The purpose of the flag is to sort files according to version number, but it works just as well here and eliminates the need to pipe the result to sort:

使用-v标志怎么样ls?该标志的目的是根据版本号对文件进行排序,但它在这里也能正常工作,并且无需将结果通过管道传输到sort

ls -lv data_*

回答by Paused until further notice.

If your sorthas version sort, try:

如果您sort有版本排序,请尝试:

ls -1 | sort -V

(that's a capital V).

(这是一个大写的 V)。

回答by Peter Lindqvist

This is a generic answer! You have to apply rules to the specific set of data

这是一个通用的答案!您必须将规则应用于特定的数据集

ls | sort

Example:

例子:

ls | sort -n -t _ -k 2

回答by Willy-André Daniel

Here's the way to do it in bash if your sort doesn't have version sort:

如果您的排序没有版本排序,这是在 bash 中执行此操作的方法:

cat <your_former_ls_output_file> | awk ' BEGIN { FS="_" } { printf( "%03d\n",) }' | sort | awk ' { printf( "data_%d_box\n", )  }'

All in one line. Keep in mind, I haven't tested this on your specific data, so it might need a little tweaking to work correctly for you. This outlines a good, robust and relatively simple solution, though. Of course, you can always swap the cat+filename in the beginning with an the actual ls to create the file data on the fly. For capturing the actual filename column, you can choose between correct ls parameters or piping through either cut or awk.

全部在一行中。请记住,我尚未在您的特定数据上对此进行测试,因此可能需要稍作调整才能为您正常工作。不过,这概述了一个良好、强大且相对简单的解决方案。当然,您始终可以将开头的 cat+filename 与实际的 ls 交换以动态创建文件数据。为了捕获实际的文件名列,您可以在正确的 ls 参数或通过 cut 或 awk 的管道之间进行选择。

回答by Vito De Tullio

maybe you'll like SistemaNumeri.py("fix numbers"): it renames your

也许你会喜欢SistemaNumeri.py("fix numbers"):它重命名你的

data_1_box
data_2_box
...
data_10_box
...
data_99_box

in

data_01_box
data_02_box
...
data_10_box
...
data_99_box

回答by Saravanan

One suggestion I can think of is this :

我能想到的一个建议是:

for i in `seq 1 5`
do  
   cat "data_${i}_box"
done