Perl : Perl equivalent to the ksh | and ;

Ive been trying to move to Perl. It has been a struggle.

My question is, is there a good resource that explains nesting statements.

As an example.

To change
primary
Factory CTS 1.9.0(46) P1
*Slot 1 CTS 1.10.2(42) P1

To
primary *Slot 1 CTS 1.10.2(42) P1

while(<$in>){
    print $out $_ if /^primary$/ .. /\*Slot /;
}

The code above only gives me whats shown in blue. I want to perform a few more functions in order to arrive at the result shown in red.

Again, Im looking to learn how to nest commands. Of course in this case probably s/// and tr.

Thanks in advance.

Assuming you have this input and you want the following output

$ cat xyz
primary
Factory CTS 1.9.0(46) P1
*Slot 1 CTS 1.10.2(42) P1
primary
Factory CTS 2.0.0(18) P1
*Slot 1 CTS 2.1.0(42) P1
$        
$ perl -lne ' if(/^primary$/ .. /\*Slot\s/) { if(/\*Slot\s/) { print join(" ", @l); undef @l; next;} push @l, $_; } ' xyz                  
primary Factory CTS 1.9.0(46) P1
primary Factory CTS 2.0.0(18) P1

Or use the following code within your script

while(<$in>) {
        if(/^primary$/ .. /\*Slot\s/) {
                if(/\*Slot\s/) { print "\n"; next; }
                s/[\r\n]/ /g; 
                print ;
        }
}
1 Like

Depends what you mean by 'nesting statements'.

It would be monstrously inefficient to use pipes to edit individual lines even in shell.

There is nothing stopping you from simply editing the data as much as you please inside a loop however.

Another way to do it is by changing the record separator and removing the newlines:

perl -0 -ne 's/\n//g; print "$1 $2\n" if /(primary).*(\*Slot.*)/'

To change
primary
Factory CTS 1.9.0(46) P1
*Slot 1 CTS 1.10.2(42) P1

To
primary *Slot 1 CTS 1.10.2(42) P1

Simplified, to do this in shell

z=`sed '/Factory/d' xyz |  tr '\012' ' '

Notice how piping the commands together I easily applied the desired result to "z".

Lets go one step further, if I wanted a specific field, again simplified

z=`sed '/Factory/d' xyz |  tr '\012' ' ' | awk  '{print $5}'

Here Id isolate the version and put in in "z"

Im sure a perl wizard would be able to do this in about as many characters as I used in shell. This is what Im after. My question is not so much related to how to execute this particular function, but how to learn how to nest/pipe commands together.

Many thanks to Mr. Bean.

With his help/code I came up with this that provides the result that I was looking for.

#!/usr/bin/perl
#
use strict; use warnings; use Data::Dumper;
open my $in,'<',"codecs";
open my $out,'>',"File2.txt";
while(<$in>){
 if(/^primary$/ .. /\*Slot\s/) {
   if(/\*Slot\s/) { print "$_"; next; }
   s/[\r\n]/ /g;
   print unless /Factory/ ;
 } elsif  (/^left$/ .. /\*Slot\s/) {
    if(/\*Slot\s/) { print "$_"; next; }
    s/[\r\n]/ /g;
    print unless /Factory/ ;
 } elsif (/^right$/ .. /\*Slot\s/) {
    if(/\*Slot\s/) { print "$_"; next; }
    s/[\r\n]/ /g;
    print unless /Factory/ ;
 }
}

Which coincidentally doesnt read as well for me anyway as

clear
echo "Parse show version"
echo "------------------"
p=`sed -n '/^primary$/,/\*Slot /p' codecs | sed '/Factory/d' | tr '\012' ' '| awk '{print $1" "$5}'`
l=`sed -n '/^left$/,/\*Slot /p' codecs | sed '/Factory/d' | tr '\012' ' '| awk '{print $1" "$5}'`
r=`sed -n '/^right$/,/\*Slot /p' codecs | sed '/Factory/d' | tr '\012' ' '| awk '{print $1" "$5}'`
echo "$p : $l : $r" 
echo " "

But Ill force myself to learn perl if it kills me... and it probably will :slight_smile:

thanks again to all ...

---------- Post updated at 08:48 AM ---------- Previous update was at 08:46 AM ----------

Sorry .. one other thing. A resource Im using, Impatient Perl, refers to Capturing and Clustering. I think that answers the real question and where I need to focus some attention.