Piping in Perl

Hi All,

I am trying to perform the below csh code in Perl, but i am unfamiliar with Perl. Can anybody give me some advice on it ?

Csh Code:

cat filename |grep AAA| grep BBB| awk '{print("already_appended")'

 
perl -lne'print "already_appended" if /AAA/ and /BBB/' filename
if($string =~ m/(AAA|BBB|CCC)/)
  {print "We've got a match!\n"}

in the most common case.

Hi sysgate,

Think if($string =~ m/(AAA|BBB|CCC)/) is an OR function.

In short, the purpose of the below csh shell actually grabs the lines which contain both the term AAAand BBB.
However, the Perl code which you showed me will only grab those lines containing either AAA or BBB or both.

Can anybody give me some advice ?
Was wondering if the below Perl code can work ?

 if($_ =~ "AAA" && $_ =~ "BBB" ) 
{print "We've got a match!\n"}

Yes, just like in the code Radoulov already posted above. ($_ is the default target for matching, so Radoulov left it out.)

Hi Era,

What if the terms we want to match is an array ?
I tried the below and it doesn;t seem to work.

Eg

 if($_ =~ @array_x[$num] && $_ =~ @array_y[$num] ) 
{print "We've got a match!\n"}
 if (/$array_x[$num]/ && /$array_y[$num]/) {
   print "We've got a match!\n"
  }

Hi radoulov,

Seems that the below Perl code, even without array term, it doesn;t work.
Can you help ?

Input File:

AAA GGG TTT
BBB TTT FFF
AAA BBB TTT YYY
AAA BBB

Shell

$ cat file| grep AAA| grep BBB
AAA BBB TTT YYY
AAA BBB

Perl

#!/usr/bin/perl

open(FH, "< file");
        while (<FH>) {
                if(/AAA/ && /BBB/ ) {
                        print $_ ; print "\n"; last ;};
        };
close FH;

Why you're using last?

zsh-4.3.4% cat file
AAA GGG TTT
BBB TTT FFF
AAA BBB TTT YYY
AAA BBB
zsh-4.3.4% cat p
#!/usr/bin/perl

open(FH, "< file");
        while (<FH>) {
                if(/AAA/ && /BBB/ ) {
                       print }
        };
close FH;
zsh-4.3.4% ./p
AAA BBB TTT YYY
AAA BBB

And as I said, you could write it as:

zsh-4.3.4% perl -lne'print if /AAA/ and /BBB/' file
AAA BBB TTT YYY
AAA BBB

Hi radoulov,

Thanks for the guidance!
It's working now.

However, I am confused about the below array syntax.
Which is exactly the right one if we want to know the value.

@array[$num] VS $array[$num]

Certainly $array[$index] is the correct way of referring to a single element of the array.
As a reminder, think of what you want to obtain; a single value, thus use the sigil $.
The other notation @array[@index_list] is an array slice.
Here you usually want to get several array elements, thus use the sigil @.
Though @array[$index] still would work, it is considered bad style
because you are after only a single element and thus there's no need for a slice.
As this is confusing a lot of Perl beginners, the language designers of Perl 6
I think will change the syntax to the sigil @ even when referring to a single element.
But I am not sure since Perl 6 is still work in progress.
And we only know that it will be out by Xmas, but we still don't know which Xmas.
But once it is out every day will be like Xmas.

Hi Buffoonix,

Thanks alot for sharing!!
Appreciate that !
:slight_smile: