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

Dear Monks,

i've been doing the following for years in KSH with something like the below but need to do it in Perl now. i have been trawling your site and the web for days now and am totally lost on how to do it in a perl.pl script.

Tried numerous while and foreach (<MYFILE>) examples and using split on ":" etc etc.. They all seem to either print the below every time for every line multiple times (like a cartesian join) of grabled mess. I have so many hacks that have failed i dont want to bother you with them. basically we need to do the following in PERL that we are currently doing in KSH.

Any assistance would be most greatly appreciated.

br&thnx, karl

Here's the KSH with for loop, cut, grep:

#!/bin/ksh for t in $(cat $MYFILE) do job_name=`echo $t | grep job_name | cut -d: -f2` days_of_week=`echo $t | grep days_of_week | cut -d: -f2` start_times=`echo $t | grep start_times | cut -d: -f2` printf "$job_name:$days_of_week:$start_times" done

----- Example (Target Output) ------

myservername001.dlog.xyz:su,mo,tu,we,th,fr,sa:"12:00" myservername001.osaudit.xyz:su,mo,tu,we,th,fr,sa:"12:00" myservername001.expdb.xyz:su,mo,tu,we,th,fr,sa:"12:00"
----- MYFILE (Source DATA) --------- /*----- myservername001.dblogs.xyz -----*/ job_name:myservername001.dlog.xyz days_of_week: su,mo,tu,we,th,fr,sa start_times: "12:00" /*----- myservername001.osaudit.xyz -----*/ job_name:myservername001.osaudit.xyz days_of_week: su,mo,tu,we,th,fr,sa start_times: "12:00" /*----- myservername001.expdb.xyz -----*/ job_name:myservername001.expdb.xyz days_of_week: su,mo,tu,we,th,fr,sa start_times: "12:00"

Replies are listed 'Best First'.
Re: [KSH convert to PERL] Rollup multi line values to single row outputs.
by BrowserUk (Patriarch) on Jan 26, 2015 at 16:31 UTC

    scriptname $MYFILE > OTHERFILE

    #! perl -slw use strict; my $line; while( <> ) { chomp; m[^/\*] and next; m[^job_name:\s*(.+?)$] and $line = $1 .':'; m[^days_of_week:\s*(.+?)$] and $line .= $1 . ':'; m[^start_times:\s*(.+?)$] and print $line . $1; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: [KSH convert to PERL] Rollup multi line values to single row outputs.
by GotToBTru (Prior) on Jan 26, 2015 at 16:46 UTC

    The solution BrowserUK provided takes advantage of several Perl features that make it such a useful language but might be confusing to the (Perl) beginner. I strongly encourage you to study it, experiment with it, and ask questions, until you feel confident you know how it works.

    Dum Spiro Spero