Sed one liners

Find every file from the current folder and replace string :
find ./ -type f -exec sed -i 's/string1/string2/g' {} \;

How to prepend text quickly in one line multiple files :
find . -name "*.txt" -exec sed -i '1s;^;written by Harlok\n;' {} \;

Grep every file containing matchstring in somedir then replace string1 by string2
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

grep every file recursively from the current folder wich contains the word foo but not with filename bar and replace string
grep -rl foo . | grep -v bar | xargs sed -i 's/string1/string2/g'

2020-10-23 22:33:14

Comments

Add a Comment

Login or Register to post a Comment.

Homepage