How to exclude multiple line patterns using diff?

ⅰ亾dé卋堺 提交于 2019-12-12 03:29:27

问题


I want to do a diff over two xml files but ignore 2-3 line patterns.

eg: Say I want to ignore availability & price while comparing the xml format below.

Here is what I have so far:

diff -I '^<PRICE>*' 1.xml 2.xml

<CATALOG>

    <PLANT>  
    <COMMON>Bloodroot</COMMON>  
    <BOTANICAL>Sanguinaria canadensis</BOTANICAL>  
    <ZONE>4</ZONE>  
    <LIGHT>Mostly Shady</LIGHT>  
    <PRICE>$2.44</PRICE>  
    <AVAILABILITY>031599</AVAILABILITY>  
    </PLANT>  
</CATALOG>

Using the above diff, it skips the price but how do I add availability to this regex?


回答1:


Run the files through grep -v to remove the lines you don't need and then diff them as shown below:

diff <(grep -vE "^(<PRICE>|<AVAILABILITY>)" 1.xml) <(grep -vE "^(<PRICE>|<AVAILABILITY>)" 2.xml)

<(...) syntax is called Process Substitution. If your shell does not support it, you will have to use temporary files, like this:

$ grep -vE "^(<PRICE>|<AVAILABILITY>)" 1.xml > 1.new.xml
$ grep -vE "^(<PRICE>|<AVAILABILITY>)" 2.xml > 2.new.xml
$ diff 1.new.xml 2.new.xml

I don't think diff -I can be used with multiple patterns.




回答2:


Did you try using -I again?

This works for me:

diff -I 'PRICE' -I 'AVAILABILITY' 1.xml 2.xml


来源:https://stackoverflow.com/questions/15497658/how-to-exclude-multiple-line-patterns-using-diff

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