Select Record based on First Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select Record based on First Column
# 1  
Old 07-12-2007
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...
# 2  
Old 07-12-2007
Code:
egrep '^I01' input_file > I01_File
egrep '^I02' input_file > I02_File

# 3  
Old 07-12-2007
Code:
awk -F, '{file=$1 "_file"; print > file; close (file)}' input_file

# 4  
Old 07-14-2007
Code:
awk -F"," '{ print > $1 }' inputfile

# 5  
Old 07-14-2007
Quote:
Originally Posted by Shell_Life
Code:
egrep '^I01' input_file > I01_File
egrep '^I02' input_file > I02_File

Why do you need egrep when only a single pattern is specified in the regex ? Smilie
# 6  
Old 07-14-2007
Quote:
Originally Posted by matrixmadhan
Code:
awk -F"," '{ print > $1 }' inputfile

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.
# 7  
Old 07-14-2007
Quote:
Originally Posted by matrixmadhan
Why do you need egrep when only a single pattern is specified in the regex ? Smilie
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:
Code:
>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.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

2. Shell Programming and Scripting

awk to select lines with maximum value of each record based on column value

Hello, I want to get the maximum value of each record separated by empty line based on the 3rd column of each row within each record? Input: A1 chr5D 634 7 82 707 A2 chr5D 637 6 82 713 A3 chr5D 637 5 82 713 A4 chr5D 626 1 82 704... (4 Replies)
Discussion started by: yifangt
4 Replies

3. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

4. Shell Programming and Scripting

Select record having different value in second column

I want records which have more than one and different value in the second column on the below sample file. Ex, I have the samle file below :- XYZ 1 XYZ 3 abc 1 abc 1 qwe 2 qwe 1 qwe 3 I want to select XYZ and QWE line only. (6 Replies)
Discussion started by: Sanjeev Yadav
6 Replies

5. Shell Programming and Scripting

Select only top "N" records based on column value

Hi Gurus, I know this'll be simple task for all the geeks out here but me being a newbie is finding it hard to crack this shell. Ok coming to the task I've a delimited file as below ================================================== ==================================================== ... (8 Replies)
Discussion started by: asandy1234
8 Replies

6. Shell Programming and Scripting

Awk - how to select the column based on day of month

Problem, How can you pass today's date (eg) 18 into Awk and select the field representing the 18th column? I have an output that I want to interact with based on what day it is information=0:0 192:0 5436:0 22:99 0:0 0:0 1234:0 1359:09 DAY=date'+%d' numbrs=`$information | awk... (2 Replies)
Discussion started by: Gizmo1007
2 Replies

7. Shell Programming and Scripting

Select record with MAX value in MySQL

Hi there, I have trouble selecting record that contain one biggest value for a group of other values. I mean, this is my table: mysql> SELECT * FROM b; +----+------+-------+ | id | user | value | +----+------+-------+ | 1 | 1 | 100 | | 3 | 1 | 150 | | 5 | 1 | 300 | | 6... (20 Replies)
Discussion started by: chebarbudo
20 Replies

8. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies

9. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question