unix - Bash find execute process with output redirected to a different file per each -


i'd run following bash command every file in folder (outputting unique json file each processed .csv), via makefile:

csvtojson ./file/path.csv > ./file/path.json 

here's i've managed, i'm struggling stdin/out syntax , arguments:

find ./ -type f -name "*.csv" -exec csvtojson {} > {}.json \; 

help appreciated!

you're passing single argument csvtojson -- filename run.

the > outputfile isn't argument @ all; instead, it's instruction shell parses , invokes relevant command connect command's stdout given filename before starting command.

thus, above, redirection parsed before find command run -- because that's place shell involved @ all.

if want involve shell, consider doing follows:

find ./ -type f -name "*.csv" \   -exec sh -c 'for arg; csvtojson "$arg" >"${arg}.json"; done' _ {} + 

...or, follows:

find ./ -type f -name '*.csv' -print0 |   while ifs= read -r -d '' filename;     csvtojson "$filename" >"$filename.json"   done 

...or, if want able set shell variables inside loop , have them persist after exit, can use process substitution avoid issues described in bashfaq #24:

bad=0 good=0 while ifs= read -r -d '' filename;   if csvtojson "$filename" >"$filename.json";     (( ++good ))   else     (( ++bad ))   fi done < <(find ./ -type f -name '*.csv' -print0)  echo "converting csv files json: ${bad} failures, ${good} successes" >&2 

see usingfind, particularly complex actions section , section on actions in bulk.


Comments