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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 11:51 AM
Moving file to directory based on condition. ramanagh Shell Programming and Scripting 2 02-02-2008 11:41 AM
Read file based on condition sbasetty Shell Programming and Scripting 5 02-01-2007 02:54 AM
Splitting a file based on some condition and naming them srivsn Shell Programming and Scripting 1 12-07-2005 11:27 AM
awk script to split a file based on the condition superprogrammer Shell Programming and Scripting 12 06-14-2005 03:59 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 02-20-2008
sivasu.india sivasu.india is offline
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 11:18 AM..
  #2 (permalink)  
Old 02-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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?
  #3 (permalink)  
Old 02-20-2008
sivasu.india sivasu.india is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
only 3 possibilities

Only three possibilities as mentioned above by me
  #4 (permalink)  
Old 02-20-2008
nj78 nj78 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 103
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
  #5 (permalink)  
Old 02-20-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #6 (permalink)  
Old 02-20-2008
nj78 nj78 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 103
Excellent addition of the error checking.
Closed Thread

Bookmarks

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 04:55 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