tags:

views:

69

answers:

3

Hi everyone, I need to search a phrase in multi files and display the results on the screen.

grep "EMS" SCALE_*
| sed -e "s/SCALE_//" -e
"s/_main_.*log:/ /" 

Since I am not get familiar with sed, I change it to Perl for convenient use.

grep "EMS" SCALE_*
| perl -e "s/SCALE_//" -e
"s/_main_.*log:/ /"

or

grep "EMS" SCALE_*
| perl -e "s/SCALE_//; s/_main_.*log:/ /"  

However , the last one is compiled but returns nothing on the command line. Any suggestion for modifying my code. Great thanks!

+2  A: 

if you want to use Perl, then try this entirely in Perl

while ( <> ){
    chomp;
    if ( $_ =~ /EMS/ ){
        s/SCALE_//g;
        s/main.*log://g;
        print $_."\n";
    }
}

on the command line

$ perl perl.pl SCALE_*

or this

$ perl -ne 'if (/EMS/){ s/SCALE_//g;s/main.*log://g; print $_} ;' SCALE_*
ghostdog74
+3  A: 

You're not looping over the input lines with perl. Check out -p or -n in the perlrun man page.

jackrabbit
+7  A: 

To use perlin the style of sed you should try the -pflag:

perl -p -e "s/SCALE_//;" -e "s/_main_.*log:/ /;"

From the explanation in perlrun:

-p

causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed:

LINE:
  while (<>) {
    ...             # your program goes here
  } continue {
    print or die "-p destination: $!\n";
  }
Bruno De Fraine