Select Record based on First Column

Hi,

I have a file with multiple records...and I have to select records based on first column....here is the sample file...

I01,abc,125,1a2,LBVI02
I01,abc,126,2b5,LBVI02
I02,20070530,254,abc,LLBI01
I02,20070820,111,bvd,NGBI01

I need all records with I01 in first field in one file and all records with I02 in one file...

egrep '^I01' input_file > I01_File
egrep '^I02' input_file > I02_File
awk -F, '{file=$1 "_file"; print > file; close (file)}' input_file
awk -F"," '{ print > $1 }' inputfile

Why do you need egrep when only a single pattern is specified in the regex ? :slight_smile:

This won't work. It will print all matching records to the file with the same name as $1, the OP wanted the newest record only, which judging by the sample input is the first occurrence only.

Matrixmadhan,
Thanks for verifying my solution.
You are right about it -- for this specific case, I don't really need 'egrep'.

Matrix, I have a habit of always use 'egrep' no matter what.

This comes from the fact that I have had problems trying to use some full
regular expressions with 'grep' -- I had to specify the '-E' option.

Sometimes I found that out after a long headache.

From my last disaster, I promise myself to never use 'grep' again, always 'egrep'.

Matrix, also to reinforce my idea, all three versions of 'grep' in my system all have the same hardlink:

>ls -li grep egrep fgrep
329785 -r-xr-xr-x  7 root  bin  22.1K Apr 12 09:11 egrep
329785 -r-xr-xr-x  7 root  bin  22.1K Apr 12 09:11 fgrep
329785 -r-xr-xr-x  7 root  bin  22.1K Apr 12 09:11 grep

Which leads me to believe that they are all the same program.

Hope to have answered your question.

Cheers.:slight_smile:

No, this is not correct.

This is what OP had asked for and the solution I had provided would work.

2 records in the file I01 and 2 records in the file I02.

Why do you think it won't work ?

Sorry guys, you're both right. I got confused between two browser windows I has open and though I was looking at this thread