Say I have this (puppet) file with an indentation of 4 spaces (I have a bunch of them that I have to process):
# init.pp
class hardwareid (
$package_name = $hardwareid::params::package_name,
$package_category = $hardwareid::params::package_category,
$package_ensure = $hardwareid::params::package_ensure
) inherits hardwareid::params {
package { "${package_name}":
name => $package_name,
category => $package_category,
ensure => $package_ensure,
}
}
I want to use sed to replace each occurence of 4 spaces at the beginning of a line with 2 spaces, to get this result:
class hardwareid (
$package_name = $hardwareid::params::package_name,
$package_category = $hardwareid::params::package_category,
$package_ensure = $hardwareid::params::package_ensure
) inherits hardwareid::params {
package { "${package_name}":
name => $package_name,
category => $package_category,
ensure => $package_ensure,
}
}
All I came up so far ist this:
sed -i -e 's/^\s\{4\}/ /g' init.pp
but this will not only replace occurences of 1x4 Spaces and therefore not include deeper indentations.
Is there a regex which can replace each 4xspace at beginning of a line with 2xspace? Is that even possible with simple regexes and sed, or do I have to switch to awk/perl/python/ruby, since I have to count the occurences to replace them with the same number?
EDIT
This question is stupid (although for a simple case, it all works). But I should not format my code without a tool that does understand the language of my code (which is Puppet). Even if I have the perfect regex (like provided inside the answers), I have the problem that if I accidentally apply the regex more than 1 time, the indentation is broken again. The Puppet guys are working on that issue: http://projects.puppetlabs.com/issues/8031 until it is solved, I have to be careful when converting files. Or write a real formatter myself (which should not be that hard).
^
(start of line)...