grep to a tab

grep to a tab

Post by Dave Packar » Thu, 31 Dec 1998 04:00:00



I have not had any luck getting grep to search only the first column of
a tab delimited database. I have tried grep ^.*\t to no avail - it
doesn't seem to recognize the \t - am I doing something wrong?

Or is there a better way to search just the first column of this
database?

 
 
 

grep to a tab

Post by brian moo » Thu, 31 Dec 1998 04:00:00


On Wed, 30 Dec 1998 14:36:30 -0800,

Quote:> I have not had any luck getting grep to search only the first column of
> a tab delimited database. I have tried grep ^.*\t to no avail - it
> doesn't seem to recognize the \t - am I doing something wrong?

Well, it's unclear why you'd want to match '^.*\t', since that's will
match any line with a tab in it.

Quote:> Or is there a better way to search just the first column of this
> database?

grep '^bob\t' filename

It's not so much the \t: that works fine and dandy with my grep: it's
the regex you're using:

    ^          match at start of line
    .*         0 or more bytes
    \t         followed by a tab

That will match any and every line with a tab, exactly like grepping for
\t itself, but slower.

--
Brian Moore                       | "The Zen nature of a spammer resembles
      Sysadmin, C/Perl Hacker     |  a*roach, except that the*roach
      Usenet Vandal               |  is higher up on the evolutionary chain."
      Netscum, Bane of Elves.                 Peter Olson, Delphi Postmaster

 
 
 

grep to a tab

Post by Dave Packar » Thu, 31 Dec 1998 04:00:00


Actually what I was trying to do was match ^.*22.*\t (change 22 to whatever you
want). The first column of the database is the product number and I want to be
able to find any product with "22" in the product number, but not in the
description, price, etc.

My grep won't find any lines if I do \t though, although I know there are tabs in
the file. It also won't find [:space:] either... Any ideas?


> On Wed, 30 Dec 1998 14:36:30 -0800,

> > I have not had any luck getting grep to search only the first column of
> > a tab delimited database. I have tried grep ^.*\t to no avail - it
> > doesn't seem to recognize the \t - am I doing something wrong?

> Well, it's unclear why you'd want to match '^.*\t', since that's will
> match any line with a tab in it.

> > Or is there a better way to search just the first column of this
> > database?

> grep '^bob\t' filename

> It's not so much the \t: that works fine and dandy with my grep: it's
> the regex you're using:

>     ^          match at start of line
>     .*         0 or more bytes
>     \t         followed by a tab

> That will match any and every line with a tab, exactly like grepping for
> \t itself, but slower.

> --
> Brian Moore                       | "The Zen nature of a spammer resembles
>       Sysadmin, C/Perl Hacker     |  a*roach, except that the*roach
>       Usenet Vandal               |  is higher up on the evolutionary chain."
>       Netscum, Bane of Elves.                 Peter Olson, Delphi Postmaster

 
 
 

grep to a tab

Post by Michael Pow » Thu, 31 Dec 1998 04:00:00


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[posted and mailed]

    Dave> I have not had any luck getting grep to search only the
    Dave> first column of a tab delimited database. I have tried grep
    Dave> ^.*\t to no avail - it doesn't seem to recognize the \t - am
    Dave> I doing something wrong?

    Dave> Or is there a better way to search just the first column of
    Dave> this database?

Use a character class, like this:  grep '^.*22.*[       ]' filename

(Linux 2.2.0-pre1) [/home/michael]
 69 --> grep '^.*22.*[  ]' greptest
line22d34       asdfasdfadsfasdfsadfasdfasdf
line122 adfjjk;jk;jk;j;klj;k

Inside the char class brackets, put an actual tab.  If you're using
bash, you'll need to do C-v first, this turns off shell interpretation
for the next key pressed; so it's C-v <TAB> to get a tab into the
brackets.

I think it will also work w/o the brackets, but it seems a little
clearer to me with the brackets in.

If you're doing a lot of processing in this regard, you may want to
consider using awk.  That's what it was made for.

(Linux 2.2.0-pre1) [/home/michael]
 90 --> awk '/.*22.*[   ]/{print $0}' greptest
line22d34       asdfasdfadsfasdfsadfasdfasdf
line122 adfjjk;jk;jk;j;klj;k

Awk, e.g., will allow to to pick and choose what fields from each
match you want to print out.

mp

8<---------------how-easy-is-it-to-demunge-an-address?------------------->8

while ($line = <>){ if ($line =~ m/^\s*$/ ){ last; }
if ($line =~ m/^From: (\S+) \(([^()]*)\)/){ $from_address = $1; } }
if ($from_address =~ m/\S+NOSPAM\S+/){ $x = index($from_address, NOSPAM);
substr($from_address, $x, 6+1) = ""; printf("The real address is %s\n",
$from_address);}else { printf("No munge, just plain %s\n",$from_address);}
printf("\nBrought to you by the Truth In Mail Headers Foundation\n");
8<-----------------------here's-one-example------------------------------>8

- --
                             Michael Powe

                         Portland, Oregon USA

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBNosDxLajuNi/6Js3EQIcFgCgyPjAwGcnEo732KEwqgfuCi+t2BIAoNA3
yXZShdtYR7GS1nKvoxh1bY+m
=g1PA
-----END PGP SIGNATURE-----

 
 
 

grep to a tab

Post by brian moo » Fri, 01 Jan 1999 04:00:00


On Wed, 30 Dec 1998 15:05:24 -0800,

Quote:> Actually what I was trying to do was match ^.*22.*\t (change 22 to whatever you
> want). The first column of the database is the product number and I want to be
> able to find any product with "22" in the product number, but not in the
> description, price, etc.

Well, that's a bit tricker: assuming your file looks like:
productnumber   price   description     quantity        vendorcode

The regex above will match any line that has a 22 in any column except
the last.  Not what you want at all.

What you want is much more difficult.  But you could do it with perl
simply enough:

perl -nae 'print if $F[0] =~ /22/'

Quote:> My grep won't find any lines if I do \t though, although I know there are tabs in
> the file. It also won't find [:space:] either... Any ideas?

Weird.  I'm using the exact same grep I've used since 1995.  Try egrep?

--
Brian Moore                       | "The Zen nature of a spammer resembles
      Sysadmin, C/Perl Hacker     |  a*roach, except that the*roach
      Usenet Vandal               |  is higher up on the evolutionary chain."
      Netscum, Bane of Elves.                 Peter Olson, Delphi Postmaster

 
 
 

grep to a tab

Post by James Youngma » Fri, 01 Jan 1999 04:00:00



> I have not had any luck getting grep to search only the first column of
> a tab delimited database. I have tried grep ^.*\t to no avail - it
> doesn't seem to recognize the \t - am I doing something wrong?

> Or is there a better way to search just the first column of this
> database?

Pipe the input data through "cut -f1".
man cut

--

 
 
 

grep to a tab

Post by gjohn.. » Mon, 04 Jan 1999 04:00:00



> Actually what I was trying to do was match ^.*22.*\t (change 22 to
> whatever you want).  The first column of the database is the product
> number and I want to be able to find any product with "22" in the
> product number, but not in the description, price, etc.

> My grep won't find any lines if I do \t though, although I know there
> are tabs in the file.       It also won't find [:space:]  either...  Any
> ideas?

There are two problems with your pattern.  The first is that grep (at
least the version I'm using, GNU grep 2.2) doesn't recognize \t as a
tab.  You may have to use a real tab character here.  The second is that
.* will match any character, including a tab.  Consequently, this
pattern will also match a 22 in the price field, for example, as long as
that isn't the last field in the line.  To match a 22 anywhere in the
first field, use

    ^[^\t]*22

Note that I have used \t for tab for readability.  To match a 22 in the
second field, use

    ^[^\t]*\t[^\t]*22

Gary

 
 
 

grep to a tab

Post by Daniel Buettne » Tue, 05 Jan 1999 04:00:00



> I have not had any luck getting grep to search only the first column of
> a tab delimited database. I have tried grep ^.*\t to no avail - it
> doesn't seem to recognize the \t - am I doing something wrong?
> Or is there a better way to search just the first column of this
> database?

It seems everyone has forgotten about our good friend awk.  To find
all occurences of, say 314, in the first column and print the entire
line that the match is found on, all you need to do is

        awk '{if($1=="314") print $0}' file.name

That should do the trick.

HTH,
--
~
~
~
"Daniel Buettner" line 4 of 4 --100%--

 
 
 

1. Script GURUs - a tab is a tab is a tab

I have ascii files created by various people I want to run something
that will look for tabs and based on the tab length convert tabs to the
right number of spaces, however the above people use various tab
lengths and do their indenting a little differently. Does someone out
there have a solution to this type of situation?????


you might have a solution.

Thanks in advance

Jim

2. Enable root access to telnet with krb5-telnet

3. how do i grep for tab slash combo?

4. stopping an alert

5. A quick question: Does 'grep' support TAB escape char ?

6. Mysqld daemon won't die

7. TAB in grep

8. 2.1.97 kernel

9. Grepping a TAB Space

10. grep for TAB ?

11. grep -E "^( |\t)* - why isn't \t a tab character?"

12. grep sTerm * | grep -v grep ???

13. tail | grep | grep | grep