1

I have little problem with deleting multiple lines with sed. I read a lot of guides and discussions, but none of them helped. I have a xml file, and I need to delete more occurences of these three lines:

<function type="class">
   <arg name="class.name">com.mycompany.name.UnLockIssueFunction</arg>
</function>

I wanted to use deleting just the part between <function>, but there is more of this tags. My xml file looks like this

<post-functions>
  <function type="class">
    <arg name="class.name">com.mycompany.name.function.issue.UpdateIssueStatusFunction</arg>
  </function>
  <function type="class">
    <arg name="class.name">com.mycompany.name.function.misc.CreateCommentFunction</arg>
  </function>
  <function type="class">
    <arg name="class.name">com.mycompany.name.function.issue.GenerateChangeHistoryFunction</arg>
  </function>
  <function type="class">
    <arg name="class.name">com.mycompany.name.function.issue.IssueReindexFunction</arg>
  </function>
  <function type="class">
    <arg name="class.name">com.mycompany.name.function.event.FireIssueEventFunction</arg>
    <arg name="eventTypeId">13</arg>
  </function>
  <function type="class">
    <arg name="class.name">com.mycompany.name.UnLockIssueFunction</arg>
  </function>
</post-functions>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jamalissimo
  • 127
  • 3
  • 13
  • You want to delete whatever tha comes between `` and `` or including the tags too? – askmish Oct 25 '12 at 06:25
  • Do you have to do this with `sed`? It would be much better to use a scripting language with a DOM parsing library. – Barmar Oct 25 '12 at 06:31
  • [Use an XML parser](http://stackoverflow.com/a/1732454/647772) –  Oct 25 '12 at 07:04

2 Answers2

2

This might work for you (GNU sed):

sed '/<function type="class">/!b;N;N;/<function type="class">\s*\n\s*<arg name="class.name">com.mycompany.name.UnLockIssueFunction<\/arg>\s*\n\s*<\/function>/d' file
potong
  • 55,640
  • 6
  • 51
  • 83
1

When you get into fancier editing, fancier editors may help. Try reading up on scripting in VIM and take a look at these questions:

Search and delete multiple lines

VIM: globally match line, delete this and 2 following lines

Community
  • 1
  • 1
Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76