473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Add same columns of a file and then compare two file contents

I have two files.

p10.txt

perform testing
Date: Nov-29-2012
Host: amg
Machtype: x86e_win64
mode: 7
ver: 1400
Build : A-01-10

-------------------------------------------------------------------
testcase Version x-20-17
-------------------------------------------------------------------
Retrieve_generic_assembly_(CP) 00:01:23
Retrieve_assembly_1_(CP) 00:01:34
Retrieve_assembly_2_(CP) 00:01:44
-------------------------------------------------------------------
xxx 00:47:03
yyy 00:47:31
************************************************** **
************************************************** **
-------------------------------------------------------------------
ggg Version P-20-17
-------------------------------------------------------------------
Retrieve_generic_assembly_(CP) 00:01:25
Retrieve_assembly_1_(CP) 00:01:35
Retrieve_assembly_2_(CP) 00:01:42
-------------------------------------------------------------------
xxx 00:47:03
yyy 00:47:31
************************************************** **
************************************************** **
-------------------------------------------------------------------
ggg Version P-20-17
-------------------------------------------------------------------
Retrieve_generic_assembly_(CP) 00:01:25
Retrieve_assembly_1_(CP) 00:01:35
Retrieve_assembly_2_(CP) 00:01:42
-------------------------------------------------------------------
pp 00:47:02
kk 00:47:36
************************************************** **
************************************************** **

p20.txt

perform testing
Date: Nov-29-2012
Host: amg
Machtype: x86e_win64
mode: 7
ver: 1400
Build : A-01-10

-------------------------------------------------------------------
testcase Version x-20-17
-------------------------------------------------------------------
Retrieve_generic_assembly_(CP) 00:01:20
Retrieve_assembly_1_(CP) 00:01:32
Retrieve_assembly_2_(CP) 00:01:41
-------------------------------------------------------------------
xxx 00:47:03
yyy 00:47:31
************************************************** **
************************************************** **
-------------------------------------------------------------------
ggg Version P-20-17
-------------------------------------------------------------------
Retrieve_generic_assembly_(CP) 00:01:25
Retrieve_assembly_1_(CP) 00:01:35
Retrieve_assembly_2_(CP) 00:01:42
-------------------------------------------------------------------
xxx 00:47:03
yyy 00:47:31
************************************************** **
************************************************** **
-------------------------------------------------------------------
ggg Version P-20-17
-------------------------------------------------------------------
Retrieve_generic_assembly_(CP) 00:01:25
Retrieve_assembly_1_(CP) 00:01:35
Retrieve_assembly_2_(CP) 00:01:42
-------------------------------------------------------------------
pp 00:47:02
kk 00:47:36
************************************************** **
************************************************** **

I want output.txt file in following format

1- To add and take average of second column entry(time stamp in hh:mm:ss) in file p10.txt (eg Retrieve_generic_assembly_(CP) is coming three times. So add all 3 entry and take average).

Similarly do it for p20.txt.

2- Write a new file output.txt that should have entry for
all first column and avg entry of file p10.txt in second column and avg entry of p20.txt in third column and take difference of second and third column in fourth column.

I want output.txt format like this in 4 column

Subsection P10 P20 Delta(p10-p20)
Retrieve_generic_assembly_(CP) 00:01:25 00:01:26 -00:00:01
Retrieve_assembly_1_(CP) 00:01:35 00:00:45 00:00:50
Retrieve_assembly_2_(CP) 00:01:42 00:02:46 -00:01:04
-----------------------------------------------------------

I am trying to achieve it through following code.
Expand|Select|Wrap|Line Numbers
  1. my $file1 = 'p10.txt';
  2. my $file2 = 'p20.txt';
  3. my $REPORT_FILE = 'outfile.txt';
  4.  
  5. my %HoA;
  6. open (R, $file1) or  die ("Could not open $file1!");
  7. while(<R>){
  8.    chomp;
  9.    my ($k, $v) = split(/\s+/);
  10.    push @{$HoA{'R'}{$k}},$v;
  11. }
  12. close(R);
  13. open (P, $file2) or die ("Could not open $file2!");
  14. while(<P>){
  15.        chomp;
  16.        my ($k, $v) = split(/\s+/);
  17.        push @{$HoA{'P'}{$k}},$v;
  18.     }
  19.     close(P);
  20.     #print Dumper \%HoA;
  21.  open FILE, ">$REPORT_FILE" or  die $!;
  22.     print FILE "Testcase Name                    P-20-17  P-20-17_system     diff\n------------------------------------------------------------------\n";
  23.     foreach my $R (keys %{ $HoA{'R'} }) {
  24.        if (exists $HoA{'P'}{$R}) {
  25.           print FILE "$R";
  26.           foreach my $classR ( @{$HoA{'R'}{$R}} ) {
  27.              foreach my $classP ( @{$HoA{'P'}{$R}} ) {
  28.                 printf FILE "\t%-10s%-10s%s\n",$classR,$classP,$classR -$classP;
  29.              }
  30.           }
  31.           print FILE "\n";
  32.        }
  33.        else {
  34.           print FILE "$R";
  35.           foreach my $classR ( @{$HoA{'R'}{$R}} ){   
  36.              printf FILE "\t%-10s%-10s%s\n",$classR,"Not Run",           "N/A";
  37.           }
  38.  
  39.        }
  40.     }
  41.  
  42. close(FILE);
Please help to change my code to achieve the required format
Dec 6 '12 #1
1 1818
Rabbit
12,516 Expert Mod 8TB
You haven't said what's wrong with the code. Are you getting errors? What are they? Is it not doing what you want? What is it doing instead? What debugging steps have you done?
Dec 6 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
2
by: armandotop | last post by:
I am writing a project for a boat rental. I have done the shell to spring off to other modules to satisfy the menu. I am stuck on getting the right script for userid and password. I have a text...
4
by: Sunit Joshi | last post by:
Hello All I'm trying to figure out how to do this the best way. Basically, I need to compare DateTime of files across TimeZones. The situation is like this: 1. I have a database say in Korea...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
1
by: somen | last post by:
How to compare the contents of two tables of identical structure?
0
by: mlco | last post by:
Pls help. I have 2 wafer maps file for comparison. The checking will only start after the "#" and ended with "##". The file contains; ERROR CODE OK WAFER ID 01 REFERENCE DIE ROWS 74
7
pureenhanoi
by: pureenhanoi | last post by:
Hi bro! Can anyone tell me, how to compare two strings like Operating System compare file names. Example: if i have two string "T*.DOC" and "TIET1.DOC". The comparision operator must return TRUE...
0
by: svpriyan | last post by:
hai, I could not understand how to debug the error i have now. target 1- read the files from directory // i able ot do this part 2- for each file for each file read the content &...
12
by: Aditya sant | last post by:
Am spliting file string into array by using fgets(),and explode() On other hand am taking string from user,and again spliting it into array now I have 2 array when I compare 2 arrays by using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.