开发者

Replace a text by shell programming

开发者 https://www.devze.com 2023-01-19 04:43 出处:网络
i have more than 1000 files and want to replace a special text in all of them with another phrase. how can i do it by 开发者_开发技巧shell script in linux?sed -i \'s/old-word/new-word/g\' *.txt

i have more than 1000 files and want to replace a special text in all of them with another phrase.

how can i do it by 开发者_开发技巧shell script in linux?


sed -i 's/old-word/new-word/g' *.txt

http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/


Something like this:

for file in *.txt
do
    cp $file $file.tmp
    cat $file.tmp | sed 's/foo/bar/g' > $file
done


You could also use perl:

perl -pi -e 's/find/replace/g' *.txt


Just bash

for file in *.txt
do
   while read -r line
   do
     case "$line" in
       "*pattern*") line="${line//pattern/new}";;
     esac
     echo "$line"
   done <"$file" > t
   mv t "$file"
done


Use sed.

0

精彩评论

暂无评论...
验证码 换一张
取 消