Search code examples
bashshellawksedgrep

replace XML tags value in shell script


Hi I am a xml file like this.

  <parameter name="END_DATE">20181031</parameter>

I want to replace this tags value to some other value I tried like this.

dt=$(awk -F '[<>]' '/_DATE/{print $3}' test.xml)

I extracted the tags value.

I have another variable value like this.

newdt=20181108

Now I need to replace this value to the extracted value.

Any help would be appreciated.


Solution

  • Though Chepner is right that awk or sed are not exact tools for xml in case you are NOT having xmlstarlet in your system then try following.

    echo $newdt
    20181108
    awk -v dat="$newdt" 'match($0,/>[0-9]+</){$0=substr($0,1,RSTART) dat substr($0,RSTART+RLENGTH-1)} 1' Input_file