How to ignore lines starting with a string with diff?
Asked Answered
S

3

19

How can I diff two files and ignore lines starting with a sequence.

E.g

File1:
abc
def
time:123

File2:
igh
def
time:345

With unix diff it will report

<time:123
>time:345

I want to ignore this diff. Any ideas?

Selah answered 30/4, 2010 at 19:26 Comment(0)
O
23

How about: diff -I '^time.*' file1 file2?

Please note it doesn't always work as expected as per diffutils manual:

However, -I only ignores the insertion or deletion of lines that contain the regular expression if every changed line in the hunk (every insertion and every deletion) matches the regular expression.

In other words, for each non-ignorable change, diff prints the complete set of changes in its vicinity, including the ignorable ones. You can specify more than one regular expression for lines to ignore by using more than one -I option. diff tries to match each line against each regular expression, starting with the last one given.

Octavia answered 30/4, 2010 at 19:32 Comment(0)
H
4

What about this?

diff <(grep -v '^time:' file1) <(grep -v '^time:' file2)
Hack answered 29/5, 2020 at 10:0 Comment(0)
S
0

Tagging onto nc3b's answer, you can also specify -I multiple times like:

diff -I time -I version file1 file2
Senility answered 29/4, 2024 at 20:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.