in reply to Re: How to Extract Data from Text file 1 based on the Value from Text file 2
in thread How to Extract Data from Text file 1 based on the Value from Text file 2

Hi Serf, Thanks a lot, the code is working as I need to. Right Now, I'm trying to put the each customer reports to each separate txt or html file.
Can please give a hint or show how to do it?
Thanks a Lot!
  • Comment on Re^2: How to Extract Data from Text file 1 based on the Value from Text file 2

Replies are listed 'Best First'.
Re^3: How to Extract Data from Text file 1 based on the Value from Text file 2
by serf (Chaplain) on Mar 04, 2010 at 13:49 UTC
    If you change the last block to this it should do what you want.

    Modify the "$output" filename to suit your needs.

    for my $customer ( keys %$cust ) { # If you want to write to a file named with the customer ID # my $output = "./$customer.out"; # If you want to write to a file named with the customer name my $output = "./$cust->{$customer}->{name}.out"; open (CUST_OUT, ">$output") || die "Can't write to '$output': $!\n +"; print CUST_OUT "CUSTOMER NAME: $cust->{$customer}->{name}\n" . "Device_Code Port Traf_Dir Data_Usage\n"; for my $device ( sort by_name keys %{$bill_data->{$customer}} ) { for my $port ( sort keys %{$bill_data->{$customer}->{$device}} + ) { (my $short_port = $port) =~ s/^FastEthernet/Fa/g; for my $direction qw(OUT IN) { print CUST_OUT "$device $short_port $direction " . $bill_data->{$customer}->{$device}->{$port}->{$dir +ection} . $/; } } } close CUST_OUT; }
      Thanks a lot Serf, very much appreaciate your help... I should continue my work from here.

      Thanks again.
      Hi Serf, I was trying to add another column which is the last month period in the report output. I was able to get the last month period like 1 Feb 10 - 28 Feb 10. But when I tried to add the column in the program. The new "Period" column is inserted in a diffent row.

      Current Ouput with new Period Column:

      CUSTOMER NAME: AGD-WEb
      Period Device_Code Port Traf_Dir Data_Usage
      1Feb2010 - 28Feb2010
      SWITCH Fa1_0_33 OUT1.311
      1Feb2010 - 28Feb2010
      SWITCH Fa1_0_33 IN10.716
      1Feb2010 - 28Feb2010
      SWITCH Fa1_0_35 OUT50.796
      1Feb2010 - 28Feb2010
      SWITCH Fa1_0_35 IN7.882
      1Feb2010 - 28Feb2010
      SWITCH Fa2_0_33 OUT-0.000
      1Feb2010 - 28Feb2010
      SWITCH Fa2_0_33 IN-0.000
      1Feb2010 - 28Feb2010
      SWITCH Fa2_0_35 OUT-0.000
      1Feb2010 - 28Feb2010
      SWITCH Fa2_0_35 IN-0.000

      Here's the code I updated with $lastmonth_period (I use DateTime module from CPAN) :) :
      for my $customer ( keys %$cust ) { # If you want to write to a file named with the customer ID # my $output = "./$customer.out"; # If you want to write to a file named with the customer name my $output = "./$cust->{$customer}->{name}.txt"; open (CUST_OUT, ">$output") || die "Can't write to '$output': $!\n +"; print CUST_OUT "Data Transfer Report for $cust->{$customer}->{name +}\n" . "Period Device_Code Port T +raf_Dir Data_Usage\n"; for my $device ( sort by_name keys %{$bill_data->{$customer}} ) { for my $port ( sort keys %{$bill_data->{$customer}->{$device}} + ) { (my $short_port = $port) =~ s/^FastEthernet/Fa/g; for my $direction qw(OUT IN) { print CUST_OUT "$lastmonth_period $device $short_port +$direction" . $bill_data ->{$customer}->{$device}->{$port}->{$di +rection} . $/; } } } close(CUST_OUT); }
      The output I am trying to do should be like:

      CUSTOMER NAME: AGD-WEb
      Period Device_Code Port Traf_Dir Data_Usage
      1Feb2010 - 28Feb2010 SWITCH Fa1_0_33 OUT1.311
      1Feb2010 - 28Feb2010 SWITCH Fa1_0_33 IN10.716
      1Feb2010 - 28Feb2010 SWITCH Fa1_0_35 OUT50.796
      1Feb2010 - 28Feb2010 SWITCH Fa1_0_35 IN7.882
      1Feb2010 - 28Feb2010 SWITCH Fa2_0_33 OUT-0.000
      1Feb2010 - 28Feb2010 SWITCH Fa2_0_33 IN-0.000
      1Feb2010 - 28Feb2010 SWITCH Fa2_0_35 OUT-0.000
      1Feb2010 - 28Feb2010 SWITCH Fa2_0_35 IN-0.000

      Also, I need to get the total Data Usage of each customer, so i need to get the sum. Can please help me on this? Or how should I update the code from here? I'm currently lost right now.
      Thanks a lot!
        Hi there, I found the reason why the output is going to a new line. It's because the %lastmonth_period variable has a newline at the end "\n". :)

        Now I'm left on how to get the total data usage of each customer. Appreciate some help on this. :)