Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

join lines and print

Posted on 2007-10-05
2
Medium Priority
?
1,899 Views
Last Modified: 2008-02-07
I have a small script, getting the info I want just not being able to get the format I want. I believe I need to
join the results I have but not getting that to work.


I have a file with lines in it such as:

Date is 200710010300

Total number of calendars:    10792
Total number of events:       2374732
Total number of tasks:        68125

Date is 200710020300

Total number of calendars:    10828
Total number of events:       2373163
Total number of tasks:        68119

Date is 200710030300

Total number of calendars:    10848
Total number of events:       2381772
Total number of tasks:        68096



My script is like this:

#!/usr/bin/perl

use strict;

open (IN, "totals.txt") or die qq{Failed to open file -- $!};

 while (<IN>) {
    chomp;
    next if /^\s*$/;
    my ($date) = (/Date is\s(.*?)$/);
    my ($cals) = (/Total number of calendars:\s(.*?)$/);
    my ($events) = (/Total number of events:\s(.*?)$/);
    my ($tasks) = (/Total number of tasks:\s(.*?)$/);
  ####print IN join(qq{\t},qw{$date $cals $events $tasks});
    print "$date\n" if $date;
    print "$cals\n" if $cals;
    print "$events\n" if $events;
    print "$tasks\n" if $tasks;
}


It will print out now like this:

200710010300
   10792
      2374732
       68125
200710020300
   10828
      2373163
       68119
200710030300
   10848
      2381772
       68096



What I want is for it to print out like

200710010300 10792 2374732 68125
200710020300 10828 2373163 68119
200710030300 10848 2381772 68096

I would think I just need to take my results and join them but not doing it right.


Thanks,
0
Comment
Question by:bt707
2 Comments
 
LVL 17

Accepted Solution

by:
mjcoyne earned 2000 total points
ID: 20026494
#!/usr/bin/perl -w
use strict;

open (IN, "totals.txt") or die qq{Failed to open file -- $!};

while (<IN>) {
    next if /^\s*$/;
    my ($date) = (/Date is\s+(\d+)$/);
    my ($cals) = (/Total number of calendars:\s+(\d+)$/);
    my ($events) = (/Total number of events:\s+(\d+)$/);
    my ($tasks) = (/Total number of tasks:\s+(\d+)$/);
    print "$date " if $date;
    print "$cals " if $cals;
    print "$events " if $events;
    print "$tasks\n" if $tasks;

}
0
 

Author Comment

by:bt707
ID: 20027044
Thanks mjcoyne,

That's just what I was trying to do, I just put the tabs back in and got just what I needed, I was just using the tabs so I could send the output to a xls file and be able to read the results from a excel file.

Still have a long, long way to go but at least I learn some things from here.

Thanks again!

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question