Tuesday, August 12, 2014

Diff 2 files for missing lines out of sequence

cat <(grep -x -v -F -f file2 -- <((grep -x -v -F -f file1 -- file2) >> file1;cat file1)) >> file2 

## reports any lines contained in <file 1> missing in <file 2>  
IFS=$(echo -en "\n\b") && for a in $(cat <file 1>); do ((\!$(grep -F -c -- "$a" <file 2>))) && echo $a; done && unset IFS

#!/usr/bin/env bash
if [[ $# -eq 2 ]]; then
## reports any lines contained in <file 1> missing in <file 2>  
IFS=$(echo -en "\n\b") && for a in $(cat "$1"); do ((\!$(grep -F -c -- "$a" "$2"))) && echo $a; done && unset IFS
elif [[ $# -eq 3 ]]; then
IFS=$(echo -en "\n\b") && for a in $(cat "$1"); do b=$(echo "$a" | sed "s/\(.\{$3\}\)\(.*\)/\1/");((\!$(grep -F -c -- "$b" "$2"))) && echo $a; done && unset IFS
elif [[ $# -eq 4 ]]; then
 TMPFILE1="/tmp/_lgd_$$_f1.txt"
 TMPFILE2="/tmp/_lgd_$$_f2.txt"
 (IFS=$(echo -en "\n\b") && for a in $(cat "$1"); do b=$(echo "$a" | sed "s/\(.\{$3\}\)\(.*\)/\1/");((\!$(grep -F -c -- "$b" "$2"))) && echo $a; done && unset IFS) >> "$TMPFILE1"
 cat "$TMPFILE1" >> "$2"
 (IFS=$(echo -en "\n\b") && for a in $(cat "$2"); do b=$(echo "$a" | sed "s/\(.\{$3\}\)\(.*\)/\1/");((\!$(grep -F -c -- "$b" "$1"))) && echo $a; done && unset IFS) >> "$TMPFILE2"
 cat "$TMPFILE2" >> "$1"
 rm "$TMPFILE1" "$TMPFILE2"
else
cat <<EOF
  Lgd: line grep diff, finds all the strings in file1 missing from file2 and optionally
       limits the grep search to the first # of chars
  Usage: lgd.bsh <file1> <file2> <chars> <flag sync both files>
   E.g. lgd.bsh  myaliases.bsh /tmp/old_myaliases.bsh 11 syncit
          
          lgd.bsh  oldlegalclauses.txt newlegalclauses.txt 500 syncit 
EOF
fi 

No comments:

Post a Comment