Advertisement
elirang

worddiff.sh

May 13th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. same='-' #unchanged
  4. up='β–³' #exists in first line, but not in second
  5. down='β–½' #exists in second line, but not in first
  6. reset=''
  7.  
  8. reset=$'\e[0m'
  9. same=$reset
  10. up=$reset$'\e[1m\e[7m'
  11. down=$reset$'\e[1m\e[7m\e[31m'
  12.  
  13. timeout=1
  14.  
  15.  
  16. if [[ "$1" != '' ]]
  17. then
  18.     paste -d'\n' "$1" "$2" | "$0"
  19.     exit
  20. fi
  21.  
  22. function demo {
  23.     "$0" <<EOF
  24. Paris in the spring
  25. Paris in the the spring
  26. A cat on a hot tin roof.
  27. a cant on a hot in roof
  28. the quikc brown box jupps ober the laze dogs
  29. The quickbrown fox jumps over the lazy dogs
  30. EOF
  31. }
  32.  
  33. # Change \x20 to \x02 to simplify parsing diff's output,
  34. #+   then change \x02 back to \x20 for the final output.
  35. # Change \x09 to \x01 to simplify parsing diff's output,
  36. #+   then change \x01 into β†’ U+1F143 (Squared Latin Capital Letter T)
  37. function input {
  38.     sed \
  39.         -e "s/\x09/\x01/g" \
  40.         -e "s/\x20/\x02/g" \
  41.         -e "s/\(.\)/\1\n/g"
  42. }
  43. function output {
  44.     sed -n \
  45.         -e "s/\x01/β†’/g" \
  46.         -e "s/\x02/ /g" \
  47.         -e "s/^\(.\) *\x3C$/\1 \x3C  /g" \
  48.         -e "s/\(.\) *\(.\) \(.\)$/\1\2\3/p"
  49. }
  50.  
  51. ifs="$IFS"
  52. IFS=$'\n'
  53. demo=true
  54.  
  55. while IFS= read -t "$timeout" -r a
  56. do
  57.     demo=false
  58.     IFS= read -t "$timeout" -r b
  59.     if [[ $? -ne 0 ]]
  60.     then
  61.         echo 'No corresponding line to compare with' > /dev/stderr
  62.         exit 1
  63.     fi
  64.  
  65.     diff --text -yt -W 19  \
  66.         <(echo "$a" | input) \
  67.         <(echo "$b" | input) \
  68.     | \
  69.     output | \
  70.     {
  71.         type=''
  72.         buf=''
  73.         while read -r line
  74.         do
  75.             if [[ "${line:1:1}" != "$type" ]]
  76.             then
  77.                 if [[ "$type" = '|' ]]
  78.                 then
  79.                     type='>'
  80.                     echo -n "$down$buf"
  81.                     buf=''
  82.                 fi
  83.  
  84.                 if [[ "${line:1:1}" != "$type" ]]
  85.                 then
  86.                     type="${line:1:1}"
  87.  
  88.                     echo -n "$type" \
  89.                         | sed \
  90.                             -e "s/[<|]/$up/" \
  91.                             -e "s/>/$down/" \
  92.                             -e "s/ /$same/"
  93.                 fi
  94.             fi
  95.  
  96.             case "$type" in
  97.             '|')
  98.                 buf="$buf${line:2:1}"
  99.                 echo -n "${line:0:1}"
  100.                 ;;
  101.             '>')
  102.                 echo -n "${line:2:1}"
  103.                 ;;
  104.             *)
  105.                 echo -n "${line:0:1}"
  106.                 ;;
  107.             esac
  108.         done
  109.  
  110.         if [[ "$type" = '|' ]]
  111.         then
  112.             echo -n "$down$buf"
  113.         fi
  114.     }
  115.  
  116.     echo -e "$reset"
  117. done
  118.  
  119. IFS="$ifs"
  120.  
  121. if $demo
  122. then
  123.     demo
  124. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement