tags:

views:

58

answers:

1

I have two text files with several sections in them. Each section has has a header with the section name (grep can extract all the section names without extracting anything else from the file). How can I report the differences between the two files AND report the section that the difference occurs in? I would also need to be able to report added/missing sections. Ideally, identical sections would not be mentioned in the report at all.

+1  A: 

If you introduce an artificial change in the headers, that will force them to show up in the diff. Not exactly what you want, but maybe that will give you an idea.

Assuming your regex for finding headers is ^HEAD:

sed -e 's/^HEAD/>HEAD/' file1.txt | diff -u - file2.txt

Edit: If you want the resulting diff to be a real diff, you can use sed to remove the HEAD difference lines.

sed -e 's/^HEAD/>HEAD/' file1.txt | diff -u - file2.txt | sed -e 's/^->HEAD/ HEAD/; /^+HEAD/D'
dave
This gives me an idea of a direction to go, but your second example still leaves lines of context in the output. So far I haven't been able to remove those lines of context - even with the diff '-U 0' option. I'll tinker with it some more.
Les