Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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 ( [id://826703]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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

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; }

Replies are listed 'Best First'.
Re^4: How to Extract Data from Text file 1 based on the Value from Text file 2
by roborat (Novice) on Mar 04, 2010 at 15:19 UTC
    Thanks a lot Serf, very much appreaciate your help... I should continue my work from here.

    Thanks again.
Re^4: How to Extract Data from Text file 1 based on the Value from Text file 2
by roborat (Novice) on Mar 05, 2010 at 08:35 UTC
    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. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://826703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-03-28 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found