我在名为files.txt的文本文件中具有以下文件列表:
./themes/seven/images/list-item-rtl.png
./themes/seven/images/arrow-desc.png
./themes/seven/images/arrow-prev.png
./themes/seven/images/fc-rtl.png
./themes/seven/ie7.css
./xmlrpc.php
./MAINTAINERS.txt
./update.php
./README.txt
./COPYRIGHT.txt
./web.config
我只想按文件名对它们进行排序(按最后一次出现的正斜杠后的第一个字符排序)。因此,上面的输出将如下所示:
./COPYRIGHT.txt
./MAINTAINERS.txt
./README.txt
./themes/seven/images/arrow-desc.png
./themes/seven/images/arrow-prev.png
./themes/seven/images/fc-rtl.png
./themes/seven/ie7.css
./themes/seven/images/list-item-rtl.png
./update.php
./web.config
./xmlrpc.php
我该怎么做呢?
$ awk -F '/' '{ printf "%s/%s\n", $NF, $0 }' files.txt | sort | cut -d '/' -f 2-
./COPYRIGHT.txt
./MAINTAINERS.txt
./README.txt
./themes/seven/images/arrow-desc.png
./themes/seven/images/arrow-prev.png
./themes/seven/images/fc-rtl.png
./themes/seven/ie7.css
./themes/seven/images/list-item-rtl.png
./update.php
./web.config
./xmlrpc.php
这会将文件名的副本复制到该行的开头,后跟一个/
字符。通过使用最后一个定/
界字段来获得文件名。最后一个字段$NF
在中awk
。
awk
命令的输出已排序。这种排序将使用/
添加到该行开头的-定界字段。cut
然后用于删除添加的-/
分隔字段。
只要没有路径名包含文字换行符,此方法就可以使用。
如果任何路径名都以/
字符(可能是目录的路径名)结尾,也可能会出现问题。这些将在排序列中产生一个空字符串。
另请参阅:施瓦兹变换
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句