How to use NULL (\0) as the delimiter in GNU sort

前端 未结 3 825
执念已碎
执念已碎 2021-02-19 04:45

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 conta

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 05:07

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

    Capturing output of find . -print0 into a bash array

    # 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' ' '
    

提交回复
热议问题