You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extract-contacts 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/sh
  2. SCRIPT_NAME="$(realpath "$0")"
  3. SCRIPT_PATH="$(dirname "$SCRIPT_NAME")"
  4. cd "$SCRIPT_PATH"
  5. if [ -z "$FILE" ]; then
  6. printf " Usage:\\n"
  7. printf " FILE=/path/to/contacts.vcf %s\\n" "$0"
  8. exit 0
  9. fi
  10. CACHE="cache/$(basename "$FILE").cache"
  11. if [ -f "$CACHE" ]; then
  12. cat "$CACHE"
  13. exit
  14. fi
  15. touch "$CACHE"
  16. chmod 600 "$CACHE"
  17. cat "$FILE" | while read LINE; do
  18. FLAG="$(echo "$LINE" | cut -d: -f1)"
  19. VALUE="$(echo "$LINE" | cut -d: -f2- | sed "s/\r//g" | sed "s/\n//g")"
  20. if [ "$FLAG" = "END" ]; then
  21. if [ -n "$FULL_NAME" ] && [ -n "$PHONE" ]; then
  22. printf "%s|%s\\n" "$FULL_NAME" "$PHONE"
  23. fi
  24. FULL_NAME=
  25. PHONE=
  26. export FULL_NAME
  27. export PHONE
  28. elif [ "$FLAG" = "FN" ] && [ -n "$VALUE" ]; then
  29. FULL_NAME="$VALUE"
  30. export FULL_NAME
  31. elif [ "$FLAG" = "FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE" ] && [ -n "$VALUE" ]; then
  32. FULL_NAME="$(printf "$VALUE" | perl -MMIME::QuotedPrint -pe '$_=MIME::QuotedPrint::decode($_);')"
  33. export FULL_NAME
  34. elif [ "$FLAG" = "TEL;CELL;PREF" ] && [ -n "$VALUE" ]; then
  35. PHONE="$VALUE"
  36. export PHONE
  37. elif [ "$FLAG" = "TEL;CELL" ] && [ -n "$VALUE" ]; then
  38. PHONE="$VALUE"
  39. export PHONE
  40. fi
  41. done > "$CACHE"
  42. cat "$CACHE"