]> git.plutz.net Git - lobster/commitdiff
update_attribute function
authorPaul Hänsch <paul@plutz.net>
Tue, 14 May 2019 12:21:51 +0000 (14:21 +0200)
committerPaul Hänsch <paul@plutz.net>
Tue, 14 May 2019 12:21:51 +0000 (14:21 +0200)
pdiread.sh

index d2eb9fa2501b30cbf4eeed78fd93d87513d0505f..d774ada3caedba752d1271aa1bc8ceacc662b5d5 100755 (executable)
@@ -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#*:}"
 }