Perl encontrar e substituir está escrevendo todos os arquivos

2

Aqui está o script de teste que estou executando

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

Eu preciso descobrir como ajustar essa linha:

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

Para não escrever todos os arquivos que encontrar, basta escrever os arquivos que precisam ser alterados.

    
por Matt Alexander 29.01.2011 / 21:58

2 respostas

2

Este deve ser um substituto adequado:

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

    
por 29.01.2011 / 23:41
2

Tente isto:

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

Isso procura a expressão regular e retorna nos nomes de arquivo onde ela é encontrada. Então o Perl irá operar apenas nesses arquivos.

    
por 29.01.2011 / 23:32