Issue
I'm running a command with diff
on 2 very big assembly dumps. I see lots of output like this:
903c903
< ; Emitting BLENDED_CODE for generic X86 CPU
---
> ; Emitting BLENDED_CODE for Pentium 4
995c995
< ; Emitting BLENDED_CODE for generic X86 CPU
---
> ; Emitting BLENDED_CODE for Pentium 4
1123c1123
< ; Emitting BLENDED_CODE for generic X86 CPU
---
> ; Emitting BLENDED_CODE for Pentium 4
1191c1191
< ; Emitting BLENDED_CODE for generic X86 CPU
---
> ; Emitting BLENDED_CODE for Pentium 4
1278c1278
< ; Emitting BLENDED_CODE for generic X86 CPU
---
> ; Emitting BLENDED_CODE for Pentium 4
1347c1347
< ; Emitting BLENDED_CODE for generic X86 CPU
---
> ; Emitting BLENDED_CODE for Pentium 4
1546c1546
< inc dword ptr [ebp-10H]
---
> add dword ptr [ebp-10H], 1
1552c1552
< inc esi
---
> add esi, 1
I don't really want to see the lines that contain "Pentium 4" or "generic X86 CPU"; specifically, if there is a line that contains "Pentium 4" I want to exclude that line and the 3 lines above it.
Is it possible to do this using grep
? I know about grep -v 'Pentium 4'
, but that only works for single lines. I can't do grep -v -- '---'
obviously, since there are some valid diffs that contain a triple-dash.
If the grep query were run on the above code, the result should be
1546c1546
< inc dword ptr [ebp-10H]
---
> add dword ptr [ebp-10H], 1
1552c1552
< inc esi
---
> add esi, 1
Solution
I like process substitution here. Let's assume file_a is:
; Emitting BLENDED_CODE for generic X86 CPU
inc dword ptr [ebp-10H]
And file_b is:
; Emitting BLENDED_CODE for Pentium 4
add dword ptr [ebp-10H], 1
Running
diff -I "^;" file_a file_b
will return:
1,2c1,2
< ; Emitting BLENDED_CODE for generic X86 CPU
< inc dword ptr [ebp-10H]
---
> ; Emitting BLENDED_CODE for Pentium 4
> add dword ptr [ebp-10H], 1
Which isn't exactly that you want I guess. However, when you try with process substitution i.e.
diff <(grep -v '^;' file_a) <(grep -v '^;' file_b)
you will get:
1c1
< inc dword ptr [ebp-10H]
---
> add dword ptr [ebp-10H], 1
Answered By - Dave Grabowski