Can sed replace text with a string formatted like printf's formatted printing?
The following sed command replaces a line starting with the current value of "$domain" with several values specified in variables.
/bin/sed "s/\(^${domain} *${limittype} * ${limititem}.*\)/$EXPL#\1\n${domain} ${limittype} ${limititem} ${value}/" /etc/security/limits.conf
However the output will not be properly aligned because the length of the values of domain etc. are not the same.
So the output would be something like
#oracle hard nproc 131072
oracle hard nproc 666
While valid, it is difficult to read. I would prefer to get something like
#oracle hard nproc 131072
oracle hard nproc 666
The best I can come with to get the desired output is:
/bin/sed "s/\(^${domain}\)\( *\)\(${limittype}\)\( *\)\(${limititem}\)\( *\)\(.*\)/$EXPL#\1\2\3\4\5\6\7\n${domain}\2${limittype}\4${limititem}\6${value}/" /etc/security/limits.conf
But I believe there must a more elegant way to do this.
The sed one liners document contains some examples that use a specified number of characters, e.g.
sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # set at 78 plus 1 space
But this is in the regexp
section not in the replacement
section.
$EXPL#\1\n
bit seems wrong.sed
can't do this in any way that is practical. If you can provide a clearer description of the problem, many people here can provide an alternative solution usingbash
,awk
, or any number of other tools.