Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

grep problem

1 view
Skip to first unread message

holm...@my-deja.com

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
Hi,
How can I search for a specific name in a file? Here's how the file
looks:

USER: mylogin NUM: 1
USER:my NUM:2
USER: other NUM: 3

I'm writing a script that will allow people to edit the NUM field. But
if their login is part of another person's login name then grep will
give me 2 lines (do a grep for "my" and you'll get 2 lines). To further
complicate the problem, there might be a space between USER: and the
login or there might not. Also sometimes there's a tab between the
login name and NUM: and sometimes it's spaces (I found this out when I
tried to do 'grep "my " tempfile' and it wouldn't work).

Any ideas are greatly appreciated.

shawn


Sent via Deja.com http://www.deja.com/
Before you buy.

Glenn West

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
In article <8vbt3i$q65$1...@nnrp1.deja.com>,

This seems to work:

tab=`printf "\t"`;egrep "USER:[ $tab]+my[ $tab]+" test.dat

HTH...

holm...@my-deja.com

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
That works as long as there is a space between USER: and the login. It
won't work on the 2nd line in my example. We're getting closer!

In article <8vbv90$s0j$1...@nnrp1.deja.com>,

Glenn West

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
In article <8vc1hn$u5b$1...@nnrp1.deja.com>,

holm...@my-deja.com wrote:
> That works as long as there is a space between USER: and the login. It
> won't work on the 2nd line in my example. We're getting closer!

So change the first + to an asterisk (*)...

holm...@my-deja.com

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
YOU DA MAN!

Thanks!

In article <8vc1nb$ufp$1...@nnrp1.deja.com>,

Michael M. Tung

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
holm...@my-deja.com wrote:

> Hi,
> How can I search for a specific name in a file? Here's how the file
> looks:
>
> USER: mylogin NUM: 1
> USER:my NUM:2
> USER: other NUM: 3
>
> I'm writing a script that will allow people to edit the NUM field. But
> if their login is part of another person's login name then grep will
> give me 2 lines (do a grep for "my" and you'll get 2 lines). To further
> complicate the problem, there might be a space between USER: and the
> login or there might not. Also sometimes there's a tab between the
> login name and NUM: and sometimes it's spaces (I found this out when I
> tried to do 'grep "my " tempfile' and it wouldn't work).
>

Hi Shawn,

try
rgrep "USER:.*my[\t\ ]+" <filename>

You can search for a particular group by using \(.*\) instead of ``my''
and
then recall the match with \1.

Hope this works for you.
Best,
Mike

michael_tung.vcf

elp...@my-deja.com

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to

try this: grep 'USER:.*my ' filename

In article <8vc1hn$u5b$1...@nnrp1.deja.com>,
holm...@my-deja.com wrote:
> That works as long as there is a space between USER: and the login. It
> won't work on the 2nd line in my example. We're getting closer!
>

> In article <8vbv90$s0j$1...@nnrp1.deja.com>,
> Glenn West <wes...@my-deja.com> wrote:
> > In article <8vbt3i$q65$1...@nnrp1.deja.com>,

> > holm...@my-deja.com wrote:
> > > Hi,
> > > How can I search for a specific name in a file? Here's how the
file
> > > looks:
> > >
> > > USER: mylogin NUM: 1
> > > USER:my NUM:2
> > > USER: other NUM: 3
> > >
> > > I'm writing a script that will allow people to edit the NUM field.
> But
> > > if their login is part of another person's login name then grep
will
> > > give me 2 lines (do a grep for "my" and you'll get 2 lines). To
> > further
> > > complicate the problem, there might be a space between USER: and
the
> > > login or there might not. Also sometimes there's a tab between the
> > > login name and NUM: and sometimes it's spaces (I found this out
> when I
> > > tried to do 'grep "my " tempfile' and it wouldn't work).
> > >

Glenn West

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to
In article <8vc2ek$v4q$1...@nnrp1.deja.com>,

elp...@my-deja.com wrote:
>
>
> try this: grep 'USER:.*my ' filename

And I quote:

elp...@my-deja.com

unread,
Nov 20, 2000, 3:00:00 AM11/20/00
to

missed that, try this: egrep '.*USER:[ ]?my[\t]?' filename

In article <8vc431$pv$1...@nnrp1.deja.com>,

Peter Sundstrom

unread,
Nov 20, 2000, 4:36:54 PM11/20/00
to

Glenn West <wes...@my-deja.com> wrote in message
news:8vbv90$s0j$1...@nnrp1.deja.com...

> In article <8vbt3i$q65$1...@nnrp1.deja.com>,
> holm...@my-deja.com wrote:
> > Hi,
> > How can I search for a specific name in a file? Here's how the file
> > looks:
> >
> > USER: mylogin NUM: 1
> > USER:my NUM:2
> > USER: other NUM: 3
> >
> > I'm writing a script that will allow people to edit the NUM field. But
> > if their login is part of another person's login name then grep will
> > give me 2 lines (do a grep for "my" and you'll get 2 lines). To
> further
> > complicate the problem, there might be a space between USER: and the
> > login or there might not. Also sometimes there's a tab between the
> > login name and NUM: and sometimes it's spaces (I found this out when I
> > tried to do 'grep "my " tempfile' and it wouldn't work).
> >
> > Any ideas are greatly appreciated.
>
> This seems to work:
>
> tab=`printf "\t"`;egrep "USER:[ $tab]+my[ $tab]+" test.dat

Or you can use

grep -w my <file>


Jeff Howie

unread,
Nov 21, 2000, 3:00:00 AM11/21/00
to holm...@my-deja.com
"holm...@my-deja.com" wrote:
> How can I search for a specific name in a file? Here's how the file
> looks:
>
> USER: mylogin NUM: 1
> USER:my NUM:2
> USER: other NUM: 3
>
> I'm writing a script that will allow people to edit the NUM field. But
> if their login is part of another person's login name then grep will
> give me 2 lines (do a grep for "my" and you'll get 2 lines). To further
> complicate the problem, there might be a space between USER: and the
> login or there might not. Also sometimes there's a tab between the
> login name and NUM: and sometimes it's spaces (I found this out when I
> tried to do 'grep "my " tempfile' and it wouldn't work).

Try narrowing grep's search by forcing the pattern to match entire
words:

$ grep -w my file

Would only match 'my'.
thks.jeff

0 new messages