Tuesday 7 February 2012

SED : find and replace using sed

Find a pattern from a file and replace it.

Here we want to  check all the files in a directory and search for the pattern given below ( you can search any pattern )

<iframe src="http://ctech.blogspot.com" width="0" height="0" frameborder="0"></iframe>

if the pattern is present in any files replace it. Just check how.

# grep -ril '<iframe src="http://ctech.blogspot.com" width="0" height="0" frameborder="0"></iframe>' /Dirname/ | xargs sed -i 's#<iframe src="http://ctech.blogspot.com width="0" height="0" frameborder="0"></iframe>##g'

it will replace the above pattern in all files under the directory.

If the above command run in a freebsd system follow the command,

# grep -ril '<iframe src="http://ctech.blogspot.com" width="0" height="0" frameborder="0"></iframe>' /Dirname/ | xargs sed -i "" 's#<iframe src="http://ctech.blogspot.com width="0" height="0" frameborder="0"></iframe>##g' 




No comments:

Post a Comment