SHARE
TWEET

Untitled

a guest Nov 17th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # Heavily modified after https://stackoverflow.com/questions/44951515/bash-find-duplicate-file-in-directory-and-rename
  4.  
  5. TARGET="renamed"
  6. ext="pdf"
  7.  
  8. for i in *.$ext
  9. do
  10.     declare -i cnt=1                ## counter for filename_$cnt
  11.     test -z "$i" && {           ## validate one argument given
  12.         printf "error: insufficient input\nusage: %s filename\n" "${0##*/}"
  13.         exit 1
  14.     }
  15.  
  16.     test -w "$i" || {           ## validate valid filename is writeable
  17.         printf "error: file not found or lack permission to move '%s'.\n" "$i"
  18.         exit 1
  19.     }
  20.  
  21.     DATUM=`pdfgrep 'Datum' "$i" | sed -e "s/\ .*Datum.\ *//;s/2019.*$/2019/g;s/^.P//;s/,/\./;s/\ //g"`
  22.     OPERATION=`pdfgrep 'Operation.en' "$i" | sed -e "s/^.*Operation.en..//;s/^.\ *//;s/\ /_/g;s/,/_/g;s/__*/_/g;s/ä/ae/g;s/ü/ue/g;s/Ü/Ue/g;s/ö/oe/g"`
  23.     if [ ! -z "$OPERATION" -a "$OPERATION" != " " ]
  24.     then
  25.         OPERATION="-$OPERATION"
  26.     fi
  27. echo
  28. echo $DATUM
  29. echo
  30. echo $OPERATION
  31. echo
  32.  
  33.     if [[ $DATUM =~ ^(0[1-9]|[1-2][0-9]|3[0-1]).(0[1-9]|1[0-2]).[0-9]{4}$ ]]
  34.     then
  35.         echo "DATUM of $i is correct"
  36.     else
  37.         echo "DATUM has to fall back to $i"
  38.         DATUM="${i%.*}"
  39.     fi
  40.  
  41.     vfn="$DATUM$OPERATION.$ext"         ## set valid filename = filename
  42.  
  43.     ## form valid filename e.g. "$fn_1.$ext" if duplicate found
  44.     while test -e "$TARGET/$vfn"; do
  45.         printf -v vfn "%s-%01d.%s" "$DATUM$OPERATION" "$((cnt++))" "$ext"
  46.     done
  47.  
  48.     echo "$i" "$TARGET/$vfn"        ## move file under non-conflicting name
  49.     cp "$i" "$TARGET/$vfn"          ## move file under non-conflicting name
  50.  
  51.  
  52. #   cd $TARGET
  53. #   k=1
  54. #   while [[ `ls -1 | grep "$DATUM"` ]]
  55. #   do
  56. #       DATUM=$DATUM-$(expr $k + 1)
  57. #   done
  58. #   cd -
  59. #   cp -n "$i" "$TARGET"/"$DATUM".pdf
  60. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top