The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Spliting file based on condition Raamc Shell Programming and Scripting 2 05-15-2008 08:51 AM
Moving file to directory based on condition. ramanagh Shell Programming and Scripting 2 02-02-2008 08:41 AM
Read file based on condition sbasetty Shell Programming and Scripting 5 01-31-2007 11:54 PM
Splitting a file based on some condition and naming them srivsn Shell Programming and Scripting 1 12-07-2005 08:27 AM
awk script to split a file based on the condition superprogrammer Shell Programming and Scripting 12 06-14-2005 12:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-20-2008
Registered User
 

Join Date: Feb 2008
Posts: 3
Question How to parse through a file and based on condition form another output file

I have one file say CM.txt which contains values like below.Its just a flat file

1000,A,X
1001,B,Y
1002,B,Z
...
..

total around 4 million lines of entries will be in that file.
Now i need to write another file CM1.txt which should have

1000,1
1001,2
1002,3
....
...
..

Here i am putting 1,2,3 based on condition in first file
if A + X then 1
if B+Y then 2
if B+Z then 3
These are the only three conditions that is possible.

Now can anybody suggest a way to write a Unix script which do this task a shortest possible time,since it has huge number of entries in the file.
Please give me a best solution / example that will help me .

Last edited by sivasu.india; 02-20-2008 at 08:18 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 02-20-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 934
Question do other combinations exist?

what about
A+Y
A+Z
B+X

a 2 by 3 matrix can have six ouputs
so, does the solution need to figure six or just your intitial three possibilities?
Reply With Quote
  #3  
Old 02-20-2008
Registered User
 

Join Date: Feb 2008
Posts: 3
only 3 possibilities

Only three possibilities as mentioned above by me
Reply With Quote
  #4  
Old 02-20-2008
Registered User
 

Join Date: Jul 2005
Posts: 29
Try this:

sed -e 's/A,X/1/g' -e 's/B,Y/2/g' -e 's/B,Z/3/g' CM.txt

you can put this in a file.sed and run it like so:

sed -f sedfile file


Using sed
Reply With Quote
  #5  
Old 02-20-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,235
Expanding on what nj78 said:

Code:
sed 's/A,X/1/;s/B,Y/2/;s/B,Z/3/' CM.txt > CM1.txt
1. Performance considerations

I suppose awk and sed to be about the same speed, so doing the same in awk will probably gain (or loose) some seconds per run. We found that out when dealing with other problems involving very large files here that sed's and awk's work speeds are about level and way ahead of the crowd. Other probable solutions like perl, python, shell scripts, what else, ... will be considerably slower, perhaps the slowest being shell script - it is simply not built for digesting huge loads of data.

For a discussion of this have a look here for instance.

2. Security considerations

I in your place would not be so sure about what can be and what can't - strange things happen all the time. ;-)) In your case i would not rely on your knowledge that only three combinations are possible. Even if that means a slight performance degradation i would write the sed script that way:

Code:
sed 's/A,X/1/;s/B,Y/2/;s/B,Z/3/;s/,[^123].*$/ERROR/' CM.txt > CM1.txt
The last clause does the following: all your "legal" lines are by now changed and end in either "1", "2" or "3". If a line now ends in something else ([^123] means: "neither 1 nor 2 nor 3") it was not changed by the 3 clauses before and and is therefore not a legal (expected) combination, which i flag with a big "ERROR", so it will easily be found. In a second step i would invest the time to validate my output:

Code:
if [ $(grep -c "ERROR" CM1.txt) -eq 0 ] ; then 
     print - "File passed check, everything is ok"
else
     print - "Something has gone wrong, check your file"
fi
Bottom line: it is not a problem to deal with errors as long as you are prepared for it.

I hope this helps.

bakunin
Reply With Quote
  #6  
Old 02-20-2008
Registered User
 

Join Date: Jul 2005
Posts: 29
Excellent addition of the error checking.
Reply With Quote
  #7  
Old 02-28-2008
Registered User
 

Join Date: Feb 2008
Posts: 3
Smile thnks

Its great and easy.

thanks for your reply.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0