![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 07:41 AM |
| Read file based on condition | sbasetty | Shell Programming and Scripting | 5 | 01-31-2007 10:54 PM |
| Splitting a file based on some condition and naming them | srivsn | Shell Programming and Scripting | 1 | 12-07-2005 07:27 AM |
| awk script to split a file based on the condition | superprogrammer | Shell Programming and Scripting | 12 | 06-14-2005 12:59 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 07:18 AM. |
| Forum Sponsor | ||
|
|
|
|||
|
Expanding on what nj78 said:
Code:
sed 's/A,X/1/;s/B,Y/2/;s/B,Z/3/' CM.txt > CM1.txt 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 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
I hope this helps. bakunin |