Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep problem

Status
Not open for further replies.

ianicr

IS-IT--Management
Nov 4, 2003
230
GB
I'm doing a grep on postcodes eg
grep "B2 4" filename
However this gives me results such as AB2 4 and CB2 4. I've looked at the man page and can't see how to match only the string and no extras.
Thanks
 
If you know that there will always be a comma before the post code then...

grep ",B2 4" filename

...or perhaps you can use grep -w
 
Yep the ",B2 4" seems to work fine. I've created a pattern file that looks like this:
",B24 "
",B31 "
",B32 "
",B47 "
",B54 "
",B79 9"
",B95 6"
",BA4 4"
",BA8 0"
",BA9 8"
and am trying to do a grep -f patternfile postcodefile but I get no results. However if I do them individually it finds them. Any ideas what the problem could be?
 
Does the file contain quotes ("") ? If so, then try without them.
 
Try to remove your quote in the patternfile.

tikual
 
nope. still produces nothing without quotes
 
What Unix? Can you post a few lines from patternfile, postcodesfile?
 
I'm using cygwin on winxp
patternfile:
,B24
,B31
,B32
,B47
,B54
postcodefile:
Mr,F,Fram,4 Uppergate,,,,Adeen,,AB10 13A
,F,Kram,96 Little St,,,,Adeen,,AB10 11L
,A,T1aftey,5 Hill,,,,Adeen,,AB0 3JX
,K,Linhadsfl,17 Kene St,,,,Adeen,,AB0 1QN
,T,Boydalonga,32 Kiddlandston Std,,,,Adeen,,AB30 1RT
,C,Abyounish,30 Cremanal Pl,,,,Adeen,,AB24 1RX
,R,Kirkland,2F Cremanal Pl,,,,Adeen,,AB26 17Y
,C,Galling,4e Spring St,,,,Adeen,,CB10 17B

Thanks
 
In your example lines, none of the patterns matches an enrty in the postcodes. To test it, I changed ,B24 to ,AB24 and it works fine on SCO Openserver 5.0.5. Are you aware of the space characters after the patterns? Do they exist also in the postcodes? What does cygwin manual says about -f ?
 
Sorry I only cut the top of the file. In the full file there are postcodes. Is it something to do with lf/cr in windoze/unix?
 
I think it was something to do with CR/LF's in windoze. I've ran it through a lf2cr script and it works fine now. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top