Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: iterating through a hierarchy with a filter



On Thu, Apr 10, 2008 at 12:52:45PM +0200, Thor Andreassen wrote:
[...]
> find source/ -iname '*.xml' | while read file1; do 

That should be while IFS= read -r file1

but that assumes that the file names don't contain newline
characters. -iname is a GNU extension, that's neither POSIX nor
Unix. While you're at using GNU extensions, you could use
-print0:

find source -iname '*.xml' | while IFS= read -rd$'\0' file1...

as $'\0' won't be found in a file name.

>   file2=dest/${${file1##source/}:r}.txt
>   destdir=${file2:h}
>   [[ -d $destdir ]] || mkdir -p $destdir
>   filter < $file1 > $file2
> done
[...]

This can be done in zsh with:

for file1 in **/*.(#i)xml(.NDoN); do

That builds the whole list first, but you can also do:

process() {
  file2=dest/${${1#source/}:r}.txt
  destdir=${file2:h}
  [[ -d $destdir ]] || mkdir -p -- $destdir
  filter < $file1 > $file2
  return 1
}
: **/*.(#i)xml(.NDoN+process)


POSIXly, you could do:

find source -type f -name '*.[xX][mM][lL]' -exec sh -c '
  for file1 do
    file2=${file1%.*}.txt
    file2=dest/${file2#source/}
    destdir=${file2%/*}
    [ -d "$destdir" ] || mkdir -p -- "$destdir"
    filter < "$file1" > "$file2"
  done' inline {} +

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author