Simplify sxmo_contacts.sh - use less tempfiles and join logic, output w/ colon

master
Miles Alan 5 years ago
parent f95cb51984
commit 9f930e742a
  1. 54
      scripts/core/sxmo_contacts.sh

@ -1,35 +1,29 @@
#!/usr/bin/env sh #!/usr/bin/env sh
# This script prints in reverse chronological order unique entries from the
# This script is a helper script for sxmo_modemtext.sh and sxmo_modemcall.sh # modem log merged with contact names defined in contacts file tsv.
# When this script is called from a terminal, it prints the phone's known # Wherein $CONTACTSFILE is tsv with two fields: number\tcontact name
# contacts in reverse chronological order of contact. # Wherein $LOGFILE is *sorted* tsv with three fields: date\tevt\tnumber
#
# Prints in output format: "number: contact"
CONTACTSFILE="$XDG_CONFIG_HOME"/sxmo/contacts.tsv CONTACTSFILE="$XDG_CONFIG_HOME"/sxmo/contacts.tsv
LOGFILE="$XDG_CONFIG_HOME"/sxmo/modem/modemlog.tsv LOGFILE="$XDG_CONFIG_HOME"/sxmo/modem/modemlog.tsv
SORTED_CONTACTS="$(mktemp)" contacts() {
sort -k2 "$CONTACTSFILE" > "$SORTED_CONTACTS" RECENTCONTACTEDNUMBERSREVCHRON="$(
cut -f3 "$LOGFILE" |
# Add names to numbers called/texted in modemlog tac |
tab=$(printf '\t') awk '!($0 in a){a[$0];print}' |
CALLED="$(sed 's/ +1//' "$LOGFILE" | sort -u -k3,3 | sed '/^[[:space:]]*$/d'
join -t "$tab" -1 2 -2 3 -o 2.1,1.1,2.3 -a 2 "$SORTED_CONTACTS" - | )"
sort -rk1 | cut -f2,3 | sed 's/^\t//')" RECENTCONTACTEDNUMBERSREVCHRONF="$(mktemp)"
echo "$RECENTCONTACTEDNUMBERSREVCHRON" > "$RECENTCONTACTEDNUMBERSREVCHRONF"
# add all known contacts printf %b "$(
ALL_DATA=$(printf %b "$CALLED\n$(cat "$CONTACTSFILE")" | nl) join -t"$(printf '\t')" -o1.1,2.2 -a1 -e"Unknown Number" \
"$RECENTCONTACTEDNUMBERSREVCHRONF" "$CONTACTSFILE" |
## We must now remove contacts that have called/texted from the ALL_DATA list sed 's#\t#: #g'
)"
# If there is a name for the contact, the row will have 3 columns rm "$RECENTCONTACTEDNUMBERSREVCHRONF" &
# if there is no name for the contact, the row will only have 2 columns. }
# To make sure data lines up, remove contacts called and texted duplicate named contacts seperately
NAMED_ORDER="$(echo "$ALL_DATA" | awk 'NF==3{print}{}' | sort -uk3)" contacts
NONAME="$(echo "$ALL_DATA" | awk 'NF==2{print}{}')"
RES=$(printf %b "$NAMED_ORDER\n$NONAME" | sort -k1 | cut -f2,3)
echo "$RES"
printf %b "$RES" | grep -q 8042221111 || echo "Test Number 8042221111"
rm "$SORTED_CONTACTS"

Loading…
Cancel
Save