Perlの検索と置換は各ファイルに書き込まれます。

Perlの検索と置換は各ファイルに書き込まれます。

これは私が実行しているテストスクリプトです。

matt@server:~ $ cat test.sh
#!/bin/bash
mkdir test
cd test
echo "has the string foo" > yes.txt
echo "hasn't the string" > no.txt
ls -l --time-style=full-iso
cat *
perl -e 's/foo/bar/g;' -pi $(find -type f)
ls -l --time-style=full-iso
cat *
matt@server:~ $ ./test.sh
total 8
-rw-r--r-- 1 matt matt 18 2011-01-29 13:52:17.240316663 -0700 no.txt
-rw-r--r-- 1 matt matt 19 2011-01-29 13:52:17.240316663 -0700 yes.txt
hasn't the string
has the string foo
total 8
-rw-r--r-- 1 matt matt 18 2011-01-29 13:52:17.260317727 -0700 no.txt
-rw-r--r-- 1 matt matt 19 2011-01-29 13:52:17.260317727 -0700 yes.txt
hasn't the string
has the string bar

この行を調整する方法を理解する必要があります。

perl -e 's/foo/bar/g;' -pi $(find -type f)

見つかったすべてのファイルを作成するのではなく、変更する必要があるファイルのみを作成します。

ベストアンサー1

これは適切な交換でなければなりません。

grep -l foo * | sed -e 's/[^/0-9A-Z_a-z]/\\&/g' | xargs sed -i 's/foo/bar/g'

おすすめ記事