Wednesday, June 27, 2012

sed Galore

Find and replace string recursively
find /home/penoycentral/whichfolder -type f -print0| xargs -0 sed -i 's/oldstring/newstring/g'


If you want to make sure that you are changing which, use the old grep first
find /home/penoycentral/whichfolder -type f -print0| xargs -0 grep -i oldstring


Example Problem: Change /etc/bind to /var/named/master in named.conf

Just print what will be changed
 sed -n 's/\/etc\/bind/\/var\/named\/master/p' named.conf 


to change:
 sed -i 's/\/etc\/bind/\/var\/named\/masterr/g' named.conf 


Change double qoute to single qoute
sed -i "s/[\"]/\'/g" some-file.pp


Referrence:
http://stackoverflow.com/questions/1583219/awk-sed-how-to-do-a-recursive-find-replace-of-a-string

No comments: