Problem with cat with PERL Substitute

system("cat FILENAME | perl -e 'while(<>) { print $_;}'");

system("cat FILENAME | perl -e 'while(<>) { $_ =~ s/XXX/YYY/g; print $_;}'");

First command works fine but second command gives the following error:

syntax error at -e line 1, near "{ =~"
syntax error at -e line 1, near ";}"
Execution of -e aborted due to compilation errors.

Any idea why? :confused:

  • Jingi

It works for me.

$ cat file1
line1
line2 XXX
line3
$ cat x.c
#include <stdlib.h>
main()
{
        system("cat file1 | perl -e 'while(<>) { $_ =~ s/XXX/YYY/g; print $_;}'");
        exit(0);
}
$ gcc x.c -o x
$ ./x
line1
line2 YYY
line3
$

when i put that system statement in a perl script, it gave those errors.
then this worked:

system ("cat FILENAME | perl -e 'while(<>) { s/XXX/YYY/g; print $_;}'");