bash 如何在 GNU 排序中使用 NULL (\0) 作为分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6563979/
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
How to use NULL (\0) as the delimiter in GNU sort
提问by antiplex
i am looking for a way to sort the results of findreturning 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.
我正在寻找一种方法来对find正确返回多个目录的结果进行排序,以便在 bash 脚本中进行进一步处理。由于文件名不能包含 NULL (\0) 字符,我认为这将为通过管道传输到sort.
so this is what i would expect to work as described:
所以这就是我所期望的工作方式:
find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -t $'# cf. http://mywiki.wooledge.org/BashFAQ/020
unset a i
while IFS='' read -r -d $'find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -z | tr '##代码##' '\n'
' dir; do
a[i++]="$dir" # or however you want to process each directory
done < <(find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | LC_ALL=C sort -z)
printf '%s\n' "${#a[@]}"
printf '%s\n' "${a[@]}"
# btw, you may use printf to add zero bytes
printf '%c##代码##0' g u b k | sort -z | tr '##代码##' ' '
printf '%s##代码##0' g1 u2 b3 k4 | sort -z | tr '##代码##' ' '
'
but sadly i got the compaint sort: empty tab
但遗憾的是我得到了 compaint sort: empty tab
looking around for a explanation a came across a question leading to a similar resultthat 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.
四处寻找解释时遇到了一个问题,该问题导致类似的结果,该操作将其描述为工作正常(参见 4 月 26 日的卢卡斯评论)。在我的情况下(使用 GNU sort v 7.4)这似乎不同。
i also checked the output of find by piping into od -cbut this only shows that the resulting folders are separated by NULL as expected.
我还通过管道检查了 find 的输出,od -c但这仅表明结果文件夹按预期由 NULL 分隔。
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?
这里有没有人遇到过类似的情况,并可能找到了解决方案或解释,为什么 \0 似乎是不可能的排序分隔符?
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'
编辑:请注意,这里使用 find-command 作为示例,一种更简单的测试/说明方法可能是echo "g\0u\0b\0k" | sort -t $'\0'
回答by Ignacio Vazquez-Abrams
-tis the fieldseparator. If you want to use \0as the lineseparator then you need to use -z.
-t是字段分隔符。如果要\0用作行分隔符,则需要使用-z.
回答by jeff
For further processing in a Bash script see, for example:
要在 Bash 脚本中进行进一步处理,请参见,例如:
##代码##回答by hans
Use the -zoption to sortzero-terminated data sets:
使用零终止数据集的-z选项sort:

