Skip to content

Instantly share code, notes, and snippets.

@fabacab
Created October 31, 2017 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabacab/531019dd8a1816fc5968ebb4db3b298b to your computer and use it in GitHub Desktop.
Save fabacab/531019dd8a1816fc5968ebb4db3b298b to your computer and use it in GitHub Desktop.
Bash script to separate a single vCard containing multiple contacts into multiple vCard files each with its own contact.
#!/bin/bash -
#
# Bash script to separate a single vCard containing multiple contacts
# into multiple vCard files each with its own contact.
#
# Usage:
#
# ./contact-split.sh < INPUT_FILE.vcf
#
# where INPUT_FILE.vcf is, of course, the monolithic vCard file.
#
main () {
local card_lines
local card_guid
declare -a card_lines
while read line; do
# Append each line to the end of this array.
card_lines=("${card_lines[@]}" "$line")
# Get the contact's GUID, this becomes the file name.
if [[ "$line" =~ ^X-ABUID: ]]; then
card_guid=$(echo -n "$line" | cut -d ':' -f 2)
fi
# This is the end of the card, so output it.
if [[ "$line" =~ ^END:VCARD ]]; then
printf "%s\n" "${card_lines[@]}" > "$(echo -n "$card_guid" | tr 'A-Z' 'a-z' | tr -d '\r').vcf"
unset -v card_lines
fi
done < "$1"
}
main "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment