cycloxr
cycloxr

Reputation: 387

Passing a wildcard character within a variable to use with grep in Perl?

I'm attempting to assign a variable a file path with a wildcard character in it and then using that variable in a grep command. Unfortunately when I run it, the wildcard character isn't seen. I attempted to use .* instead and even as a regex but neither worked. Any help would be appreciated.

I'm looking to grep all files that starts with ftp_log, so on the command line it would be:

grep ftpd ftp_log.* | sed 's/\*//g' > <searchfile>`

Can't seem to translate it to work in perl though.

$filename = "\/sysadm\/shared\/NCC\/logs\/" . $SYSTEM . "\/ftp_log.*";

`grep ftpd $filename | sed 's/\*//g' > $searchfile`;

grep: /sysadm/shared/NCC/logs/sslmlvfp1/ftp_log.: No such file or directory

Thanks.

Upvotes: 0

Views: 1544

Answers (1)

Mark Reed
Mark Reed

Reputation: 95315

I think you just have a pathname mismatch; your code looks like it should work - though you don't need to put backslashes in front of /s in a Perl string, and you can use interpolation instead of concatenation:

my $filename = "/sysadm/shared/NCC/logs/$SYSTEM/ftp_log.*";

That said, it's a bit silly to shell out to either grep or sed from perl. Here's one way to do the equivalent within the Perl program itself:

open my $out, '>', $searchfile;
foreach my $filename (glob "/sysadm/shared/NCC/logs/$SYSTEM/ftp_log.*") {
  open my $log, '<', $filename;
  while (<$log>) {
    if (/ftpd/) {
      s/\*//g;
      print $out $_;
    }
  }
  close $log;    
}
close $out;

Upvotes: 2

Related Questions