Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: iterating through a hierarchy with a filter
- X-seq: zsh-users 12776
- From: Stephane Chazelas <Stephane_Chazelas@xxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: iterating through a hierarchy with a filter
- Date: Thu, 10 Apr 2008 12:42:14 +0100
- In-reply-to: <20080410105245.GB11585@xxxxxxxxxxxx>
- Mail-followup-to: zsh-users@xxxxxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <8BAD0AA6-3B6F-4946-B636-6C16B56A944E@xxxxxxxxx> <200804100851.m3A8pk9S003521@xxxxxxxxxxxxxx> <DDABD6BC-F3D9-4277-86C1-776B24CB914F@xxxxxxxxx> <20080410105245.GB11585@xxxxxxxxxxxx>
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