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

Re: is text file?



On Sep 30,  9:19am, Greg Badros wrote:
} Subject: Re: is text file?
}
} ... you simply add a built-in test to zsh that is true iff that
} argument is a text file.

You could write this yourself as a module using 3.1.x.

Anyway, the following works in 3.0.5 (but not earlier because typeset -U
didn't work properly):

    text() {
      local ascii=(8 9 10 12 13 {32..126})
      local -U bytes
      for file
      do
	bytes=( $ascii $(od -An -td1 -N 256 $file) )
	[[ $#bytes -eq $#ascii ]] && echo $file
      done
    }

    grep foo $(text **/*(-.))

Increase or decrease 256 in the `od' command to get more or less accurate
guesses as to whether a file is text or not.  Adjust `ascii' if you want
to include a larger character set.  Add `[[ -f $file ]] || continue' if
you don't want to worry about the (-.) in the glob pattern.

This does assume GNU `od', I guess.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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