Issue
For example, if I want to change 424, or any number, to 1 from below string.
<revision>424</revison>
I usually do this sed -i 's|<revision>.*</revision>|<revision>777</revision>|g
and it works.
But I have to do a lot of similar commands
and I want to know if I can group like <revision>(.*)</revision>
and replace only \1
to 777
. How do I do this?
Solution
With GNU awk
and with your shown samples, please try following awk
program. Simple explanation would be, using match
function of awk
and creating 4 capturing groups in it, where 1st group captures <revision>
, 2nd one captures all Digits, 3rd one captures <\/revison>
and 4th one(if there are any other values) everything. If this match function is true then printing 1st element of arr followed by newVal
(awk variable which contains new value) followed by 3rd and 4th element value of arr.
awk -v newVal="777" '
match($0,/(<revision>)([0-9]+)(<\/revison>)(.*)/,arr){
print arr[1] newVal arr[3] arr[4]
}
' Input_file
Answered By - RavinderSingh13 Answer Checked By - Willingham (WPSolving Volunteer)