first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / text / csv_compare.sh
1 #!/bin/bash
2
3 # this script takes two CSV files and ensures that they have the same
4 # contents, regardless of the order.
5 # if the function believes the two files are the same, then zero is returned.
6 function compare_csv_files()
7 {
8   export file1=$1; shift
9   export file2=$1; shift
10
11   if [ ! -f "$file1" -o ! -f "$file2" ]; then
12     echo "One of the files is missing; for this utility to work, both:"
13     echo "\t$file1"
14     echo "\t$file2"
15     echo "need to exist before running this script."
16     return 3
17   fi
18
19   export temp_name1="$(mktemp "$TMP/zz_file1.XXXXXX")"
20   export temp_name2="$(mktemp "$TMP/zz_file2.XXXXXX")"
21   export temp_out="$(mktemp "$TMP/zz_differences.XXXXXX")"
22
23   # old code: should not be needed after 5.7.425 or so.
24   # right now we strip out realtime fields because of a problem in
25   # how they are shown (which is as a time and date).
26   #sort <"$file1" | grep -v "REALTIME" >"$temp_name1"
27   #sort <"$file2" | grep -v "REALTIME" >"$temp_name2"
28
29   # sort the two files so we can ignore the ordering.
30   sort <"$file1" >"$temp_name1"
31   sort <"$file2" >"$temp_name2"
32
33   diff "$temp_name1" "$temp_name2" >"$temp_out"
34
35   exitval=0
36
37   if [ -s "$temp_out" ]; then
38     echo "Differences seen in file:"
39     cat "$temp_out"
40     # return a failure exit value.
41     exitval=2
42   fi
43
44   rm "$temp_name1" "$temp_name2" "$temp_out"
45
46   return $exitval
47 }
48
49 # simple run using above function.
50 compare_csv_files $*
51
52