Discussion:
grepping for tabs
(too old to reply)
William Ballard
2004-04-10 20:50:08 UTC
Permalink
I searched google a bit, nothing jumped out:

Suppose the file '1' words seperated by tabs:
host cookie 8 www.execsoft.co.uk

$ grep '\b8' 1
host cookie 8 www.execsoft.co.uk
$ grep '\t8' 1
$ grep "`echo -e '\t'`8" 1
host cookie 8 www.execsoft.co.uk

I could have sworn that shells natively understood \t as tab,
but apparently the only way to pass one to a shell is with
`echo -e '\t'`, or `echo -e \\\\t`.

I must have missed something.
--
To UNSUBSCRIBE, email to debian-user-***@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact ***@lists.debian.org
dircha
2004-04-10 21:10:07 UTC
Permalink
Post by William Ballard
host cookie 8 www.execsoft.co.uk
$ grep '\b8' 1
host cookie 8 www.execsoft.co.uk
$ grep '\t8' 1
$ grep "`echo -e '\t'`8" 1
host cookie 8 www.execsoft.co.uk
I could have sworn that shells natively understood \t as tab,
but apparently the only way to pass one to a shell is with
`echo -e '\t'`, or `echo -e \\\\t`.
I must have missed something.
I think the following is what you want:

$'\tsomething'

See bash manual section on Quoting.

dircha
--
To UNSUBSCRIBE, email to debian-user-***@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact ***@lists.debian.org
s. keeling
2004-04-10 23:00:12 UTC
Permalink
Post by dircha
Post by William Ballard
host cookie 8 www.execsoft.co.uk
$ grep '\b8' 1
host cookie 8 www.execsoft.co.uk
$ grep '\t8' 1
$ grep "`echo -e '\t'`8" 1
host cookie 8 www.execsoft.co.uk
I could have sworn that shells natively understood \t as tab,
but apparently the only way to pass one to a shell is with
`echo -e '\t'`, or `echo -e \\\\t`.
$'\tsomething'
grep <CTRL>qblah 1

Control-q is the same thing you do in vi to get the literal character.
--
Any technology distinguishable from magic is insufficiently advanced.
(*) http://www.spots.ab.ca/~keeling
- -
--
To UNSUBSCRIBE, email to debian-user-***@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact ***@lists.debian.org
Bob Proulx
2004-04-10 23:00:13 UTC
Permalink
Post by William Ballard
$ grep '\t8' 1
$ grep "`echo -e '\t'`8" 1
host cookie 8 www.execsoft.co.uk
I could have sworn that shells natively understood \t as tab,
but apparently the only way to pass one to a shell is with
`echo -e '\t'`, or `echo -e \\\\t`.
Yes, a common shell question. Here are the two common ways of
creating ascii tabs on the comand line. The first is maximally
portable back to very old v7 systems and are preferred by some. The
second is my preferred method on modern systems.

TAB=`awk 'BEGIN{printf "\t";}'`
TAB=$(printf "\t")

As a bash specific feature you can use $'\t' but since that is so bash
specific I hate to use it.

But basically, yes, you have to create a variable first and then use
the variable where you need the tab. Alternatively you can create
literal tabs but it is whitespace and gets lost easily.

Bob

Loading...