Sunday, November 15, 2009

Diff command ignoring certain lines


Lets try to use some of important command line options available with UNIX/Linux DIFF command.

Contents of 'file1.txt' and 'file2.txt' are:

$ cat file1.txt
k,99,32332,10.24
p,676,211,121.44
#Some comment1
n,908,121,12121.54
#Some comment1
l,na,90.23,23
l,na,20.28,23


$ cat file2.txt
k,99,32332,10.24

p,676,211,121.44
#Some comment1
n,908,121,12121.54
#Some comment1
l,na,90.23,23
#Some more comment
l,na,20.28,23

A normal diff command output:

$ diff file1.txt file2.txt
1a2
>
3c4
< #Some comment1
---
> #Some commentr3
6a8
> #Some more comment

Some of DIFF(1) important command line options:
-q --brief
Output only whether files differ.

$ diff -q file1.txt file2.txt
Files file1.txt and file2.txt differ

-B --ignore-blank-lines
Ignore changes whose lines are all blank.

$ diff -B file1.txt file2.txt
3c4
< #Some comment1
---
> #Some commentr3
6a8
> #Some more comment

-I RE --ignore-matching-lines=RE
Ignore changes whose lines all match RE.
So in this case lets ignore all lines which start with '#'

$ diff -B -I '^#' file1.txt file2.txt
$ echo $?
0

So no difference.

A small and simple bash script function for the same:

#!/bin/sh

_Diff() {
local file1=$1
local file2=$2
echo "Diff-ing '$file1' and '$file2' \
ignoring 'blank' lines and lines \
starting with '#'"
diff -q -B -I '^#' ${file1} ${file2} > /dev/null
[ $? -eq 0 ] && echo "Passed" || echo "Failed"
}

_Diff /tmp/newsch.txt /tmp/oldsch.txt

Another good option available with DIFF(1)

-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)

Related post:

- Diff remote files using ssh in Linux

No comments:

© Jadu Saikia www.UNIXCL.com