Advertisement
metalx1000

VCARD to CSV

Mar 27th, 2023 (edited)
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.60 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2023  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. #converts VCARD file to a simple CSV with just Names and Numbers
  20.  
  21. tmp_dir="/tmp/contact_split"
  22. vcard_dir="$HOME/Documents/$(date +%Y)/phone/sync"
  23. vcard_file="${vcard_dir}/contacts.vcard"
  24. csv_file="${vcard_dir}/contacts.csv"
  25.  
  26. mkdir -p "$tmp_dir"
  27. cd "$tmp_dir" || exit 1
  28.  
  29. csplit -z "$vcard_file" /BEGIN:VCARD/ '{*}' > /dev/null
  30.  
  31. function create_csv(){
  32.   f="$1"
  33.   name="$(grep "^FN:" "$f"|sed 's/FN://g'|dos2unix)"
  34.   numbers="$(grep "^TEL" "$f"| sed 's/[^0-9]*//g'|tr "\n" ","|dos2unix)"
  35.   echo "${name},${numbers}"
  36. } >> $csv_file
  37.  
  38. echo -n "Converting Vcard to CSV..."
  39. for f in *
  40. do
  41.   echo -n "."
  42.   grep -q "^TEL" "$f" && grep -q "^FN" "$f" && create_csv "$f"
  43. done
  44.  
  45. #clean up
  46. grep -v ",$" "$csv_file"|grep -v "^[0-9]"|sort -u| sponge "$csv_file"
  47.  
  48. echo
  49. echo "$csv_file created."
  50. rm -fr "$tmp_dir"
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement