[Home] Zsh logo

text

Zsh Wizard

Download text Return to Examples
  1. # From: "Bart Schaefer" <schaefer>
  2. # Subject: Re: is text file?
  3. #
  4. # On Sep 30, 9:19am, Greg Badros wrote:
  5. # } Subject: Re: is text file?
  6. # }
  7. # } ... you simply add a built-in test to zsh that is true iff that
  8. # } argument is a text file.
  9. #
  10. # You could write this yourself as a module using 3.1.x.
  11. #
  12. # Anyway, the following works in 3.0.5 (but not earlier because typeset -U
  13. # didn't work properly).
  14. #
  15. # grep foo $(text **/*(-.))
  16. #
  17. # Increase or decrease 256 in the `od' command to get more or less accurate
  18. # guesses as to whether a file is text or not. Adjust `ascii' if you want
  19. # to include a larger character set. Add `[[ -f $file ]] || continue' if
  20. # you don't want to worry about the (-.) in the glob pattern.
  21. #
  22. # This does assume GNU `od', I guess.
  23. text() {
  24. local ascii=(8 9 10 12 13 {32..126})
  25. local -U bytes
  26. for file
  27. do
  28. bytes=( $ascii $(od -An -td1 -N 256 $file) )
  29. [[ $#bytes -eq $#ascii ]] && echo $file
  30. done
  31. }