ramthen has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks ,

Pls. guide me which way to go with respect to scenario given below.

CASE:

PERL script executes set of commands with status of each command logged in a file; output from each command varies in width and also their category

Some samples category is given here

Project status :

DB update :

How do I format these categories / lines to look orderly (as given below)?

Project Status :

DB Update :

Any pointers would be of great help?

/RamThen/

Replies are listed 'Best First'.
Re: Pointers to formatting output
by BrowserUk (Patriarch) on Jan 09, 2004 at 13:42 UTC

    Reading between the lines of your post --literally. I had to look at the source of the html to work out what you were after:) -- I think you want one of these two:

    printf "%20s: \n", $_ for 'this', 'that', 'and this other bit'; this: that: and this other bit: printf "%-20s: \n", $_ for 'this', 'that', 'and this other bit'; this : that : and this other bit :

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!

      thanks for pointers

      I followed suggestion given by BrowserUk and was able to log action as per my requirements

      Now when I mail this log using Net::SMTP module I do not get contents in same format as they are in log. I declared content-type to text in script

      From log :

      - Project : test_ch

      - Product : test

      Same log while sent as mail :

      - Project : test_ch

      - Product : test

      Does any body know reason for this behaviour ?

      /RamThen/

Re: Pointers to formatting output
by Roger (Parson) on Jan 09, 2004 at 13:39 UTC
    I see that you are looking for a way to 'capitalize' the first letter of every word? And also align the colon ':'? (You have to be more specific with your question next time, otherwise it becomes a game of 'spotting the difference' again. ;-)
    my @text = ( "Project status :", "DB update :", "Some column :", ); my $max_line_width = 0; # capitalize the first letter of every word, and record # the maximum width of the lines seen. my @formatted_text = map { s/(\w+)/\u\L$1/g; $max_line_width = length if $max_line_width < length; $_ } @text; # insert spaces before the colon ':' to pad the string to # the maximum line width recorded my @formatted_text = map { substr($_, -1, 0) = ' ' x ($max_line_width - length); $_ } @formatted_text; print "$_\n" for @formatted_text;
    And the output -
    Project Status : Db Update : Some Column :
Re: Pointers to formatting output
by Hena (Friar) on Jan 09, 2004 at 13:25 UTC
    For simple formatting (columns same size, numbers with 0 in front etc.) use printf (sprintf for internal handling of scalars). This is probably what you want. Next stage might be usage of print <<"END". That will allow scalars to be used ('END' wont).

    Most advanced way is then 'formats'. These allow much more control in what is printed where and how (man perlform).
Re: Pointers to formatting output
by Hofmator (Curate) on Jan 09, 2004 at 13:25 UTC
    I'm not sure what you are trying to achieve, but have a look at printf or here at the monastery Using (s)printf().

    -- Hofmator

Re: Pointers to formatting output
by calin (Deacon) on Jan 09, 2004 at 13:47 UTC
    Update: It looks that the OP doesn't want grouping by category ; he wants prettyfication of the lines themselves (see BrowserUk's comment above). The rest of my post below is moot.

    (hope I understand your question correctly)

    If a regexp like m/^(.+:)\s*$/ matches a category headline, and you have a logfile like:

    Project status: project created DB update: database initialized Project status: world domintation ahead DB update: database low on memory DB update: database borks ; all data lost Project status: shattered dreams Project status: starting all over again

    then the following program will give you a starting point:

    #!/usr/bin/perl my %cats; my $cat = "_UNDEFINED:"; open my $fh, '<', 'categories.txt' or die; while (<$fh>) { next if m/^\s*$/; #skip blank lines if (m/^(.+:)\s*$/) { $cat = $1; next; } $cats{$cat} .= ">> $_"; } for (sort keys %cats) { print "$_\n"; print $cats{$_}; } close $fh;

    The output is:

    DB update: >> database initialized >> database low on memory >> database borks ; all data lost Project status: >> project created >> world domintation ahead >> shattered dreams >> starting all over again