Bash 按正则表达式排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8680561/
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
Bash sort by regexp
提问by mecio
I have something about 100 files with the following syntax
我有大约 100 个文件,其语法如下
ahfsdjfhdfhj_EPI_34_fdsafasdf
asdfasdf_EPI_2_fdsf
hfdjh_EPI_8_dhfffffffffff
ffffffffffasdfsdf_EPI_1_fyyy44
...
There is always EPI_NUMBER. How can I sort it by this number?
总是有 EPI_NUMBER。我怎样才能按这个数字排序?
回答by anubhava
From your example it appears that delimiter is _and text EPI_nnncomes at the same position after delimiter _. If that is always the case then you can use following command to sort the file:
从您的示例中可以看出 delimiter is_并且 textEPI_nnn在 delimiter 之后出现在相同的位置_。如果总是这样,那么您可以使用以下命令对文件进行排序:
sort -n -t "_" -k 3 file.txt
UPDATE:
更新:
If position of EPI_text is not fixedthen use following shell command:
如果EPI_文本位置不固定,则使用以下 shell 命令:
sed 's/^\(.*EPI_\)\(.*\)$/##/' file.txt | sort -n -t "_" -k1 | sed 's/^\(.*\)##\(.*\)$//'
回答by codaddict
回答by holygeek
sed -e 's/EPI_/EPI /' file1 file2 ...|sort -n -k 2 -t ' '
Pipe that to sed -e 's/ /_/'to get back the original form.
用管道将sed -e 's/ /_/'其恢复为原始形式。
回答by potong
This might work for you:
这可能对你有用:
ls | sed 's/.*EPI_\([0-9]*\)/ &/' | sort -n | sed 's/\S* //'

