如何在GNU排序中使用NULL(\ 0)作为分隔符 [英] How to use NULL (\0) as the delimiter in GNU sort

查看:94
本文介绍了如何在GNU排序中使用NULL(\ 0)作为分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来对find的结果进行排序,以正确返回许多目录,以便在bash脚本中进行进一步处理.因为文件名不能包含NULL(\ 0)字符,所以我认为这对于将结果传递到sort而言将是一个很好的定界符.

所以这就是我期望的那样工作:

find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -t $'\0'

但是可惜我得到了compaint sort: empty tab

四处寻找解释,遇到了导致该问题的问题类似的结果,该操作被描述为运行良好(请参阅4月26日的卢卡斯评论).就我而言(使用GNU sort v 7.4),这似乎有所不同.

我还通过管道传递到od -c来检查find的输出,但这仅显示所生成的文件夹按预期被NULL分隔.

这里有没有人遇到过类似的情况,并可能找到了解决方案或解释,为什么\ 0似乎不是排序的定界符?

期待着您的答案...

编辑:请注意,此处使用find命令作为示例,测试/说明此问题的更简单方法可能是echo "g\0u\0b\0k" | sort -t $'\0'

解决方案

-t field 分隔符.如果要使用\0作为分隔符,则需要使用-z.

i am looking for a way to sort the results of find returning a number of directories correctly for further processing in a bash script. since filenames can't contain the NULL (\0) character i thought this would make a great delimiter for the results being piped to sort.

so this is what i would expect to work as described:

find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -t $'\0'

but sadly i got the compaint sort: empty tab

looking around for a explanation a came across a question leading to a similar result that the op described as working fine (see lucas comment of apr 26th). in my case (using GNU sort v 7.4) this is seems different.

i also checked the output of find by piping into od -c but this only shows that the resulting folders are separated by NULL as expected.

has anybody here come across a similar scenario and possibly found a solution or explanation why \0 seem to be an impossible delimiter for sort?

looking forward to you answers...

edit: note that the find-command is used as an example here, a simpler way to test/illustrate this could be echo "g\0u\0b\0k" | sort -t $'\0'

解决方案

-t is the field separator. If you want to use \0 as the line separator then you need to use -z.

这篇关于如何在GNU排序中使用NULL(\ 0)作为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆