如何使用NULL(\ 0)作为GNU排序中的分隔符

时间:2022-09-23 07:42:54

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.

我正在寻找一种方法来排序结果找到正确返回多个目录,以便在bash脚本中进一步处理。因为文件名不能包含NULL(\ 0)字符,我认为这将为管道排序的结果做出很好的分隔符。

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.

环顾四周寻找解释时遇到的问题导致了类似的结果,认为操作正常(参见11月26日的卢卡斯评论)。在我的情况下(使用GNU sort v 7.4),这似乎是不同的。

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.

我还检查了通过管道输入到od -c的find,但这只显示结果文件夹按预期分隔为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'

3 个解决方案

#1


11  

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

-t是字段分隔符。如果要使用\ 0作为行分隔符,则需要使用-z。

#2


2  

For further processing in a Bash script see, for example:

有关Bash脚本中的进一步处理,请参阅:

Capturing output of find . -print0 into a bash array

捕获查找的输出。 -print0进入bash数组

# cf. http://mywiki.wooledge.org/BashFAQ/020
unset a i
while IFS='' read -r -d $'\0' 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\000' g u b k | sort -z | tr '\0' ' '
printf '%s\000' g1 u2 b3 k4 | sort -z | tr '\0' ' '

#3


1  

Use the -z option to sort zero-terminated data sets:

使用-z选项对零终止数据集进行排序:

find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -z | tr '\0' '\n'

#1


11  

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

-t是字段分隔符。如果要使用\ 0作为行分隔符,则需要使用-z。

#2


2  

For further processing in a Bash script see, for example:

有关Bash脚本中的进一步处理,请参阅:

Capturing output of find . -print0 into a bash array

捕获查找的输出。 -print0进入bash数组

# cf. http://mywiki.wooledge.org/BashFAQ/020
unset a i
while IFS='' read -r -d $'\0' 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\000' g u b k | sort -z | tr '\0' ' '
printf '%s\000' g1 u2 b3 k4 | sort -z | tr '\0' ' '

#3


1  

Use the -z option to sort zero-terminated data sets:

使用-z选项对零终止数据集进行排序:

find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -z | tr '\0' '\n'