ripat
June 23, 2008, 6:43am
1
Sometimes obvious things... are not so obvious. I always thought that it was possible to grep non printable characters but not with my GNU grep (5.2.1) version.
printf "Hello\tWorld" | grep -l '\t'
printf "Hello\tWorld" | grep -l '\x09'
printf "Hello\tWorld" | grep -l '\x{09}'
None of them work. I know that work-arounds with od, cat -vet or sed are possible but why not simply with grep that is meant to do this type of job?
What am I doing wrong?
Try like this:
$ printf 'hello\tworld' | grep $'\t'
With grep you can match control characters like this:
grep '[[:cntrl:]]'
And non printable characters like this:
grep '[^[:print:]]'
Another workaround with shells expanding the special construct $'\X' (ksh93, bash, zsh):
zsh-4.3.4% printf "Hello\tWorld"|grep -l $'\t'
(standard input)
zsh-4.3.4% printf "Hello\tWorld"|grep -l $'\x09'
(standard input)
ripat
June 23, 2008, 7:25am
4
Thanks to both it works fine with the $'\x' construct in ksh and bash. Nice to know.