I have large number of files that have an deprecated css value "hspace" and I want to change it with other css style value. Here is an example: we have:


and we want to change with:

style="margin: 0 5px;"

each file has different value inside hspace, one can be hspace='5' other hspace='2', hspace='7' and so on.

what I want is find in all files in specific directory:

hspace='ANY NUMBER'

and replace with:

style="margin: 0 5px;"

Any suggestion to do this in bash centos os?

  • hspice='8' => will be transformed into what? – user218374 Apr 6 '17 at 16:49
  • with style="margin: 0 8px;" – becoolflyer Apr 6 '17 at 17:21
  • Then you need to edit your main theme where you are stating that: hspace='ANY NUMBER' to replace with: style="margin: 0 5px;" – user218374 Apr 6 '17 at 22:14

You can use this command for replacing (assuming GNU sed as found on CentOS):

sed -i "s/hspace='\([0-9]*\)'/style=\"margin: 0 \1px;\"/g" filename.html

To replace in all .html-files:

find . -type f -name '*.html' -exec sed -i "s/hspace='\([0-9]*\)'/style=\"margin: 0 \1px;\"/g" {} +
  • in my case files don't have extension. I tried to use '*' instead of '*.html' but doesn't work – becoolflyer Apr 6 '17 at 14:51
  • ok, seems that @Stéphane-Chazelas changed last solution and now it works perfectly. Thanks – becoolflyer Apr 6 '17 at 14:54
  • Sorry, i should have checked it before publishing. Thanks to @Stéphane Chazelas for edits. – ValeriyKr Apr 7 '17 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.