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

前端 未结 3 819
执念已碎
执念已碎 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 04:59

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

    0 讨论(0)
  • 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' ' '
    
    0 讨论(0)
  • 2021-02-19 05:15

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

    find ./ -maxdepth 1 -type d -iname 'xyz?' -print0 | sort -z | tr '\0' '\n'
    
    0 讨论(0)
提交回复
热议问题