The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
appending spaces to first line based on second record. ammu Shell Programming and Scripting 2 11-16-2007 03:35 AM
Select a portion of file based on query vanand420 Shell Programming and Scripting 14 10-31-2006 04:48 AM
awk to select a column from particular line number mab_arif16 Shell Programming and Scripting 4 05-08-2006 05:26 AM
Select records based on search criteria on first column shashi_kiran_v UNIX for Dummies Questions & Answers 2 12-02-2005 01:49 PM
adding a column at the end of the record pavan_test UNIX for Dummies Questions & Answers 2 11-07-2005 11:39 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-12-2007
mgirinath mgirinath is offline
Registered User
  
 

Join Date: May 2005
Posts: 69
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 (permalink)  
Old 07-12-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Code:
egrep '^I01' input_file > I01_File
egrep '^I02' input_file > I02_File
  #3 (permalink)  
Old 07-14-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Originally Posted by Shell_Life View Post
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 ?
  #4 (permalink)  
Old 07-14-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Quote:
Originally Posted by matrixmadhan View Post
Why do you need egrep when only a single pattern is specified in the regex ?
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.
  #5 (permalink)  
Old 07-12-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Code:
awk -F, '{file=$1 "_file"; print > file; close (file)}' input_file
  #6 (permalink)  
Old 07-14-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
awk -F"," '{ print > $1 }' inputfile
  #7 (permalink)  
Old 07-14-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Quote:
Originally Posted by matrixmadhan View Post
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.
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0