From: Paul Hänsch Date: Tue, 14 May 2019 12:21:51 +0000 (+0200) Subject: update_attribute function X-Git-Url: https://git.plutz.net/?p=confetti;a=commitdiff_plain;h=a76ef45bd763687be2e517955bc50089adaa64ed update_attribute function --- diff --git a/pdiread.sh b/pdiread.sh index d2eb9fa..d774ada 100755 --- a/pdiread.sh +++ b/pdiread.sh @@ -27,6 +27,11 @@ BR=' ' pdi_load() { + # normalise PDI file for processing with pdi_* functions + # functions in this library can only be applied to normalised data + # Usage example: + # data="$(pdi_load file.vcf)" + sed -En ' # === Read entire file into buffer === :X $bY; N; bX; :Y s;^.*$;\n&\n;; @@ -132,7 +137,7 @@ pdi_update_value(){ local card="${BR}$1" name="$2" cnt="$3" val="$4" while [ "$cnt" -gt 0 ]; do if [ "${card#*${BR}${name};*:}" = "${card}" ]; then - printf '%s\n%s;:' "${card%${BR}END:VCARD*}" "${name}" + printf '%s\n%s;:' "${card%${BR}END;:VCARD*}" "${name}" card="${BR}END;:VCARD" break; else @@ -143,6 +148,21 @@ pdi_update_value(){ fi cnt=$((cnt - 1)) done - printf '%s\n' "$val" - printf '%s\n' "${card#*${BR}}" + printf '%s\n%s\n' "$val" "${card#*${BR}}" +} + +pdi_update_attrib(){ + local card="${BR}$1" name="$2" cnt="$3" val="$4" + while [ "$cnt" -gt 0 ]; do + if [ "${card#*${BR}${name};*:}" = "${card}" ]; then + printf '%s\n%s;' "${card%${BR}END;:VCARD*}" "${name}" + card=":${BR}END;:VCARD" + break; + else + printf '%s\n%s;' "${card%%${BR}${name};*}" "${name}" + card="${card#*${BR}${name};}" + fi + cnt=$((cnt - 1)) + done + printf '%s:%s\n' "$val" "${card#*:}" }