9

最後にバージョン番号が付いたファイルのリストがあり、並べ替える必要があります

/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9

grep でパイプすると、次のようにソートされます。

echo $files | sort -n
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9

-n は、ファイル名の最初の数字で混乱していると思います。

最後の数字で数値的に並べ替えるにはどうすればよいですか

4

5 に答える 5

5

/列区切りとして使用できます。

sort -n -t/ -k 10
于 2013-06-05T16:31:14.557 に答える
0

または、perl や ruby​​ のようなワンライナーを使用します。

ruby -we 'print *readlines.sort_by{|x| File.basename(x).to_i}'
于 2013-06-05T16:52:19.170 に答える