0

I have an XML file which contain portion as given below.

<Target name=something>
     < other content here>
</Target>

Now how can I remove this content from my file using sed command? I tried the below but didn't work.

sed -ir  's/\<Target Name\=.*.\<\/Target\>//g' filename

Please advice.

1
  • Depending on the version of regular expressions used \< and \> may have special meaning: [<] and [>] will match the literals in all cases. But you will need to do a lot more to match patterns over multiple lines: I commend this tutorial: not easy reading, but explaining sed never can be.
    – AFH
    May 27, 2016 at 15:22

2 Answers 2

4
  1. you should use XML tools to manipulate XML structures

  2. sed is meant to process stuff on a line-by-line basis, you probably want to go for awk to solve this type of substitution.

To solve your problem with awk:

awk '/<Target /,/<\/Target>/ { next } 1' your_file

edit:

just found: sed can do the same thing

sed '/<Target /,/<\/Target>/d' your_file
2

John has actually answered the bigger part of your question. However, I noticed that you intend to do in-place editing by providing the -i option to sed. Note that this option takes an optional argument SUFFIX. If SUFFIX is provided then a backup of the input file is made by adding SUFFIX to its name. The very likely problem in you command line is that the letter 'r' in -ir is interpreted as the backup suffix, rather than a separate option -r. To achieve the desired effect (when you don't mean to backup files during in-place editing) do not combine other options with -i (or make sure that i always comes last):

# All these do the same thing (in-place editing using extended regular expressions)
sed -r -i ...
sed -i -r ...
sed -ri ...

# This one backs up input files by appending the letter 'r' to their names
# (and DOES NOT use extended regular expressions)
sed -ir ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .