The question is published on by Tutorial Guruji team.
We have an xml file (abc_lop.xml)
in which I need to remove a line which is present in a tag:
Below is an xml file, I have shorten it down as it is very big.
<HELLO version="4.2" xmlns="http://www.bacd.org/HELLO-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bacd.org/HELLO-4_2 http://www.bacd.org/v4-2/hello-4-2.xsd"> <!-- some data here --> </HELLO>
As you can see I have this line xsi:schemaLocation="http://www.bacd.org/HELLO-4_2 http://www.bacd.org/v4-2/hello-4-2.xsd"
in HELLO
tag above. I need to remove this line and keep other stuff.
As of now I am adding some header and footer to the above xml file which is shown below in my shell script and then storing it in file variable: Here $word
is abc
.
file=$(printf '%sn%sn%s' "$header" "$(cat "$path/${word}_lop.xml")" "$footer")
Now I want to make sure file variable should have xml file data but with that line remove as well from HELLO
tag.
I am using this $file
variable later on for some other purpose so I want to make sure $file
should have header, footer and that line removed as well. That line which has key=value pair will only be present once.
Answer
To remove the xsi:schemaLocation
entry, leaving the rest of the file intact:
$ sed 's/xsi:schemaLocation="[^"]*"//' "$path/${word}_lop.xml" <HELLO version="4.2" xmlns="http://www.bacd.org/HELLO-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <!-- some data here --> </HELLO>
s/xsi:schemaLocation="[^"]*"//
is a substitute command. It replaces anything matching the regex xsi:schemaLocation="[^"]*"
with nothing.
To combine this with your script:
file=$(printf '%sn%sn%s' "$header" "$(sed 's/xsi:schemaLocation="[^"]*"//' "$path/${word}_lop.xml")" "$footer")