Pick first "code" only [ grep/awk ]

All

I have a requirement to search and pick the data as below. Explained with example.

38999|4812 Highway 52 North|Rockville|55901|0196
67541|2800 Dexter Road|Northville|38999|0196

This is pipe separate data. First column represents dealer id and 4th column represents the zip code where dealer is located.

I am seeing a situation ,like above , where the numbers match and it creates a problem for me.

If i do a simple grep on '38999' , its bound to return me both rows whereas i am just looking for dealer id column.I do not want to filter data on zip code.
In above example , it should return me just the first row.

I guess 'awk' can help but i would like to understand if grep/fgrep can help here.Env is AIX 5.3.

Appreciating your time and help

Abhi

Using awk

awk -F\| '$1==38999' file

Using grep

grep "^38999|" file

Thanks....!!

Now ,let me modify the requirement.

I want to search the entire file [comprising of thousands of records] in unique way. It has to search based on first column only ,which is always going to be a dealer code i.e a number and should not return any rows where zip code matches with the dealer id...

Abhi

I posted two approaches for performing this search!

Now what have you tried and where are you stuck?

I did try both and they worked. Thanks !!

I slightly modified the requirement.

Earlier while searching ,i knew what i was searching for and i can loop the solutions you provided over a file to extract all unique matching rows...

Now ,if i say , let's search the entire file based on its first column ,which is a number of unknown length.... Whatever is the length ,search has to look upto first occurance of '|' and must return the row. Here , there is no question of 'match found' since the search is solely based on first column.

I need to handle incorrect or garbage data here ; so i must search on first column ,if found ,direct it to a file else report an error.

Once again , appreciating your time !!

Abhi