How to compare 2 files & get specific value & replace it in other file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to compare 2 files & get specific value & replace it in other file.
# 1  
Old 03-18-2010
Data How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends
I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other.
For example: I have few set data of both files here:
a.dat:
Code:
 PDE-W 2009 12 16  5 29 11.11  13.9100  92.9400  46.0   0  4.30   0  0.00  0.00  0.00   0  0.00  4.30   0   sss
 PDE-W 2009 12 28  2 15  4.87  30.6900  83.7700  10.0   0  4.40   0  0.00  0.00  0.00   0  0.00  4.40   0   aaa
 PDE-W 2009 12 29  9  1 55.31  24.3600  94.8100 124.0   0  0.00   0  0.00  5.70  0.00   0  0.00  5.70   0   bbb
 PDE-W 2009 12 29  9 30 13.19  39.2700  72.9700  10.0   0  4.40   0  0.00  0.00  0.00   0  0.00  4.40   0   aaa
 PDE-Q 2010  2 26  4 42 29.63  28.4100  86.7500  10.0   0  5.40   0  0.00  0.00  0.00   0  0.00  5.40   0   aaa
 PDE-Q 2010  2 27 23 21 13.42  35.9100  70.0500 104.0   0  5.70   0  0.00  0.00  0.00   0  0.00  5.70   0   aaa
 PDE-Q 2010  2 28 12 13 26.61   2.0500  98.9200  47.0   0  5.10   0  0.00  0.00  0.00   0  0.00  5.10   0   aaa
 PDE-Q 2010  3  5 19 19  4.95  34.9900  95.7900  45.0   0  4.80   0  0.00  0.00  0.00   0  0.00  4.80   0   aaa

The other file has data as shown below
b.dat:
Code:
200912092129A SIMEULUE, INDONESIA

  Date: 2009/12/ 9   Centroid Time: 21:29: 7.0 GMT
  Lat=   2.52  Lon=  95.75
  Depth= 12.0   Half duration= 2.4
  Centroid time minus hypocenter time:  4.0
  Moment Tensor: Expo=25  0.348 -0.235 -0.114 0.744 -0.846 0.162 
  Mw = 6.0    mb = 5.9    Ms = 5.9   Scalar Moment = 1.18e+25
  Fault plane:  strike=293    dip=9   slip=66
  Fault plane:  strike=138    dip=82   slip=94

200912290901A MYANMAR-INDIA BORDER REG

  Date: 2009/12/29   Centroid Time:  9: 1:55.7 GMT
  Lat=  24.32  Lon=  94.85
  Depth=127.3   Half duration= 1.6
  Centroid time minus hypocenter time:  1.7
  Moment Tensor: Expo=24  2.210 -3.340 1.130 -1.530 -1.850 0.212 
  Mw = 5.7    mb = 5.6    Ms = 5.6   Scalar Moment = 3.8e+24
  Fault plane:  strike=123    dip=41   slip=144
  Fault plane:  strike=242    dip=68   slip=55

201002272321A HINDU KUSH REGION, AFGHA

  Date: 2010/ 2/27   Centroid Time: 23:21:12.7 GMT
  Lat=  35.76  Lon=  69.87
  Depth=122.2   Half duration= 1.8
  Centroid time minus hypocenter time: -0.3
  Moment Tensor: Expo=24  3.010 -3.420 0.418 3.760 -0.388 1.730 
  Mw = 5.6    mb = 5.7    Ms = 5.7   Scalar Moment = 5.21e+24
  Fault plane:  strike=309    dip=24   slip=119
  Fault plane:  strike=98    dip=69   slip=78

201003131459A NIAS REGION, INDONESIA

  Date: 2010/ 3/13   Centroid Time: 14:59: 5.8 GMT
  Lat=   1.18  Lon=  96.80
  Depth= 20.2   Half duration= 2.0
  Centroid time minus hypocenter time:  2.8
  Moment Tensor: Expo=24  3.100 -1.150 -1.950 4.530 -3.680 1.860 
  Mw = 5.8    mb = 5.7    Ms = 5.7   Scalar Moment = 6.7e+24
  Fault plane:  strike=333    dip=16   slip=112
  Fault plane:  strike=130    dip=76   slip=84

The output file c.dat : after comparing this 2 files should be
Code:
 PDE-W 2009 12 16  5 29 11.11  13.9100  92.9400  46.0   0  4.30   0  0.00  0.00  0.00   0  0.00  4.30   0   sss
 PDE-W 2009 12 28  2 15  4.87  30.6900  83.7700  10.0   0  4.40   0  0.00  0.00  0.00   0  0.00  4.40   0   aaa
 PDE-W 2009 12 29  9  1 55.31  24.3600  94.8100 124.0   0  0.00   0  0.00  5.70  0.00   0  0.00  5.70   0   bbb
 PDE-W 2009 12 29  9 30 13.19  39.2700  72.9700  10.0   0  4.40   0  0.00  0.00  0.00   0  0.00  4.40   0   aaa
 PDE-Q 2010  2 26  4 42 29.63  28.4100  86.7500  10.0   0  5.40   0  0.00  0.00  0.00   0  0.00  5.40   0   aaa
 PDE-Q 2010  2 27 23 21 13.42  35.9100  70.0500 104.0   0  5.70   0  0.00  5.60  0.00   0  0.00  5.70   0   aaa
 PDE-Q 2010  2 28 12 13 26.61   2.0500  98.9200  47.0   0  5.10   0  0.00  0.00  0.00   0  0.00  5.10   0   aaa
 PDE-Q 2010  3  5 19 19  4.95  34.9900  95.7900  45.0   0  4.80   0  0.00  0.00  0.00   0  0.00  4.80   0   aaa

Now in this 2 files it will check for same dat & in time for same hour & mins
for ex:
In b.dat, The year month day hour & mins of 11row is same as the a.dat 's 2nd row 's year month day hour & mins.
Now we should check if th Mw value 5.7 of the b.dat file is thier in the 2nd row's 15th column If so leav as it is or els replace the 0.00 to what ever the value is..
Now if we apply the same logic for date 2010 2 27 & time 23 21 of b.dat & a.dat. The Mw value 5.6 is not thier in the 11thcolumn of 6th row in a.dat. Here the 0.00 should be replaced as 5.6.
Help me out using awk or simple shell program..

Last edited by vbe; 03-18-2010 at 05:51 AM..
# 2  
Old 03-18-2010
With Perl:

Code:
perl > c.dat -lane'
  if (@ARGV) {
    /^(\d{12})/ and $k = $1;
    /Mw\s*=\s*([\d.]+)/ and $_{$k} = $1;
    next;
    }
   $k = join "", map {sprintf "%02d", $_ } @F[1..5];
   exists $_{$k} 
      and s/((?:\S+\s+){14})(\S+)/$1.sprintf "%.2f", $_{$k}/e;
   print;
  ' b.dat a.dat

# 3  
Old 03-19-2010
Question

I dont know perl ...Can you please tel me the code using awk SmilieSmilie
# 4  
Old 03-19-2010
You can use it anyway ...
# 5  
Old 03-19-2010
for the same data above if i replace the date & time in b.dat as
Date: 2010/ 3/5 Centroid Time: 19:19: 4.8 GMT
Then the result s same ...
its not printing the result in c.dat correctly as
Code:
 PDE-W 2009 12 16  5 29 11.11  13.9100  92.9400  46.0   0  4.30   0  0.00  0.00  0.00   0  0.00  4.30   0   sss
 PDE-W 2009 12 28  2 15  4.87  30.6900  83.7700  10.0   0  4.40   0  0.00  0.00  0.00   0  0.00  4.40   0   aaa
 PDE-W 2009 12 29  9  1 55.31  24.3600  94.8100 124.0   0  0.00   0  0.00  5.70  0.00   0  0.00  5.70   0   bbb
 PDE-W 2009 12 29  9 30 13.19  39.2700  72.9700  10.0   0  4.40   0  0.00  0.00  0.00   0  0.00  4.40   0   aaa
 PDE-Q 2010  2 26  4 42 29.63  28.4100  86.7500  10.0   0  5.40   0  0.00  0.00  0.00   0  0.00  5.40   0   aaa
 PDE-Q 2010  2 27 23 21 13.42  35.9100  70.0500 104.0   0  5.70   0  0.00  5.60  0.00   0  0.00  5.70   0   aaa
 PDE-Q 2010  2 28 12 13 26.61   2.0500  98.9200  47.0   0  5.10   0  0.00  0.00  0.00   0  0.00  5.10   0   aaa
 PDE-Q 2010  3  5 19 19  4.95  34.9900  95.7900  45.0   0  4.80   0  0.00  5.80  0.00   0  0.00  4.80   0   aaa


Last edited by reva; 03-19-2010 at 06:21 AM..
# 6  
Old 03-19-2010
It's because I'm taking the date and time from this record:

Code:
200912092129A SIMEULUE, INDONESIA

In your data there are two records that contain the date and time for every paragraph:

Code:
200912092129A SIMEULUE, INDONESIA

  Date: 2009/12/ 9   Centroid Time: 21:29: 7.0 GMT
  Lat=   2.52  Lon=  95.75
  Depth= 12.0   Half duration= 2.4
  Centroid time minus hypocenter time:  4.0
  Moment Tensor: Expo=25  0.348 -0.235 -0.114 0.744 -0.846 0.162 
  Mw = 6.0    mb = 5.9    Ms = 5.9   Scalar Moment = 1.18e+25
  Fault plane:  strike=293    dip=9   slip=66
  Fault plane:  strike=138    dip=82   slip=94

The first one is easier to parse.
# 7  
Old 03-19-2010
Ya thanks so much..Its working now...Smilie

---------- Post updated at 05:32 AM ---------- Previous update was at 05:23 AM ----------

Now if i want to check for Ms or mb in the similar way as Mw & compare the 2 files..How to modify your program..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

2. Shell Programming and Scripting

Compare File & Copy Replace if Successful

Hi All, I have written a shell script that creates a backup of my MySQL database. The script performs the following functions: Creates a Backup of the MySQL database Compresses the Backup Copies the Backup to a Remote Server Send an E-Mail displaying the size of the Backup Removes any... (6 Replies)
Discussion started by: SalientAnimal
6 Replies

3. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

4. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

5. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

6. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

7. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

8. Shell Programming and Scripting

Compare two files A & B and accordingly modify file A

Friends, i have two huge complex files (for eg :A & B)as output , the sample contents of the files are as follows : A == ID,DATE,SUM1,SUM2,TOTAL(SUM1+2) A5066,20/04/2010,25000,50000,75000 A5049,20/04/2010,25000,60000,85000 B == ID,DATE,SUM1,SUM2,TOTAL(SUM1+2)... (2 Replies)
Discussion started by: appu2176
2 Replies

9. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

10. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies
Login or Register to Ask a Question