How to remove a line from a tag in shell script? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to remove a line from a tag in shell script? without wasting too much if your time.

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="" xmlns:xsi="" xsi:schemaLocation="">

    <!-- some data here  -->


As you can see I have this line xsi:schemaLocation="" 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.


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="" xmlns:xsi="" >

    <!-- some data here  -->


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")
We are here to answer your question about How to remove a line from a tag in shell script? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji