I am trying to add field at the end of tag using sed script. Suppose I have a tag in XML file:<book name=Sed tutorial price=250/>Now I want to add field as Book_Width=A after end of <book/> tag so that my tag becomes:<book name=Sed tutorial price=250 Book_Width=A/>I tried with sed:sed '/<book [^>]*>/ a Book_Width=A'but it gives:<book name=Sed tutorial price=250/>Book_Width=A
How to add an attribute in a tag in XML file by command-line?
command line;text processing;files;sed;xml
You should not parse xml with sed, use an xml parser like xmlstarlet instead. For your task it would be:xmlstarlet ed -O --inplace --insert /book --type attr -n Book_Width -v A xml_fileThe file content is then:<book name=Sed tutorial price=250 Book_Width=A/>The ed means edit mode to edit the xml tree-O omits the xml tagWe want to insert something with --insert/book is the path where to insert--type attr: it's an attribute, we want to insertThe name -n of the attributeThe value -v