简体   繁体   中英

sort files found by command 'find'

My program takes a list of files and processes them according to the order in which the files are given. For instance:

$ ./myScript.sh --takeFiles a b c d e f g

Now, since I have to pass a considerable number of files, I use the find command and specify how to find the exact files I want:

sudo find . -path "./aFolder/*_parameterOne_*_*/*_parameterTwo_*_*/*_someCommonString_*" ! -name "*_aStringToExclude*" -exec ./myScript.py --takeFiles {} +

It works like a charm, except that I would my files to be passed to myScript.sh after being sorted first by "parameterTwo_* " (where in the star I have an integer) and then by "parameterTwo *_" where again the star stands for a numerical value.

Is it possible?

The the parts before parameterOne and parameterTwo do not contain the character _ , you can simply use sort :

find ... -print0 |
sort -z -t_ -k6n -k3n |
xargs -r0 ./myScript.py --takeFiles

Update: A more complex solution might look as follows. However, I think it would be easier to sort the pathnames in the Python script.

#! /bin/bash
find ... -print0 |
while IFS= read -r -d '' pathname; do
    [[ "$pathname" =~ "_parameterOne_"([0-9]+).*"_parameterTwo_"([0-9]+) ]] &&
    printf '%05d%05d %s\0' "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" "$pathname"
done |
sort -z |
while IFS= read -r -d '' pathname; do
    printf '%s\0' "${pathname#* }"
done |
xargs -r0 ./myScript.py --takeFiles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM