join lines and print

Posted on 2007-10-05
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:


use strict;

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

 while (<IN>) {
    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:


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.

Question by:bt707
    LVL 17

    Accepted Solution

    #!/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;


    Author Comment

    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!


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now