Reputation: 3606
I have an XML node like this:
<point type="2D" x="61" y="273" />
I wish to multiply x
by 2 using Bash. I had tried the following:
echo '<rect key="frame" x="61" y="273" width="199" height="21"/>' | sed "s/x=\"\([[:digit:]]*\)\"/x=\"$((\1 * 2))\"/"
But it failed with:
syntax error: operand expected (error token is "\\1 * 2")
Any idea how to make this work?
Upvotes: 0
Views: 62
Reputation: 784878
sed
is not the the right tool for this. You can use this gnu awk command with a custom record separator:
awk -v RS='.*x="|".*' '!NF{ s=RT } NF{ print s $1*2 RT }' file
<point type="2D" x="122" y="273" />
However it is better to use a proper XML parser for thorough XML parsing.
Upvotes: 3
Reputation: 1963
This is quite easy to achieve with perl:
perl -p -e 's/x="([0-9]+)"/"x=\"".($1*2)."\""/e' input.xml
To replace directly, add -i like you would with sed.
Upvotes: 1