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

Re: is text file?



"Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxx> writes:

> On Sep 28,  3:31pm, Jose Unpingco wrote:
> } I usually use PERL's -T in a function to check if a file is ASCII
> } or binary. Is there a way to do this using zsh.
> 
> I'm sure Larry Wall will forgive me for saying that perl's -T is a hack.
> It reads a chunk of the file and guesses whether the whole file is ASCII
> based on the contents of that fragment.
> 
> Zsh's globbing uses only information from readdir() and stat()/lstat(),
> and hopefully is going to stay that way.
> 

I disagree.  The reason perl's -T exists *even though* it is such a hack
is because it is damn useful.  I'm sick of accidentally grepping through
binaries, and would love a zsh feature that would let me do:

grep foo *(T.) # search for foo in all non-binary files

Why must zsh's globbing restrict itself to only filesystem
meta-information?  Yes, I understand a (T) glob modifier would be slower
since zsh would have to read the first bit of the file (another seek)
but who cares -- users time in being able to restrict the set of files
glob more usefully is worth a lot.  Yes, there are other ways to
restrict a glob in a similar way (for example, using a script that echos
its arguments after removing names of binary files [using file]), but
they are far worse hacks than letting zsh do it for you.

Perhaps it would seem less hacky if there were a general
user-programmable glob feature that would call a function on each
filename and accept that file for the glob iff the function returns
0.  Then the way that you determine what kind of file a filename points
to is not part of the shell, but the nice glob modifier interface is
permitted. 

> An approximation might be (with extendedglob set):
> 
> % ls **/*~*(${~${(j/|/)fignore}})(.)
> 
> That is, all plain files that do not have extensions listed in `fignore'.
> You could change (.) to (.^*) to omit executables, but that would also
> omit most shell scripts.
> 
> (Somebody tell me why the extra ${~...} is needed in that expression.)

I'm fairly certain I'll never type such an incantation (how long did it
take to dream it up? :-) ).

Greg J. Badros
gjb@xxxxxxxxxxxxxxxxx
Seattle, WA  USA
http://www.cs.washington.edu/homes/gjb



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