sed delete multiple lines

社会主义新天地 提交于 2019-12-08 09:19:32

问题


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>

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/13062746/sed-delete-multiple-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!