Valerio
Valerio

Reputation: 285

string substitution in many files

I am using the command

perl -pe 's/ReplaceThis/`cat T1.php`/ge' -i T1.php.txt

to substitute the string "ReplaceThis" in the file T1.php.txt with the content of the file T1.php, and it works fine. But I would like to do this for hundreds of files at once, Ti.php for 1<=i<=700. I am trying

for file in *.php; do perl -pe 's/ReplaceThis/`cat "$file"`/ge' -i 
"$file".txt ; done

but I get the error: cat: : No such file or directory
How can I get this to work?

Upvotes: 1

Views: 53

Answers (1)

ikegami
ikegami

Reputation: 386561

Don't try to generate Perl code from a shell script! Use some other means (args, env, fd, file) to pass the information to the script.

One solution:

for file in *.php; do
    perl -i -pe'
       BEGIN { local $/; $repl = <STDIN>; }
       s/ReplaceThis/$repl/g;
    ' "$file".txt <"$file"
done

Upvotes: 2

Related Questions