A simple crm on the command-line with abook, awk [WIP] 2

3 replies [Last post]
Staircase
Offline
Joined: 02/24/2022

Hello, I am trying to put together a script for a CRM (Customer Relationship Management) on the command-line to work along with abook.

I have already all my contacts in abook, that's why I am trying to put together a script that reads the ini file 'addressbook' abook produces.

I am iterating.

That's where I am at for now:

#!/bin/sh
awk '
BEGIN {
RS = "";
FS = "\n"
}
/'$*'/
' /home/user/.abook/addressbook

This script lives in the file ~/bin/crm.

With this script, I can run:

$ crm london

This will return all the records which include 'london'.

If I only want the name of the record/contact to be returned, I run:

$ crm london | egrep name | cut -d= -f2

This has shortcomings though.

1/ The script does not match 'London'; I need to figure how to ignore case distinction, like with -i in grep.

2/ the script returns records for which 'london' isn't necessarily equals to the field 'city'; it would return the record of another field equals to 'london', for example the field 'custom1'.

That's work in progress.

Feedback welcome.

I will post updates here.

PS: I had posted a first update about this, but the post somehow did not show up in the list of threads: https://trisquel.info/en/forum/trisquel-users; this is it: https://trisquel.info/en/forum/write-awk-script-query-abooks-ini-file-simple-crm-command-line

Staircase
Offline
Joined: 02/24/2022

I am using this now:

$ crm london > tmp.address && abook --datafile tmp.address

to display the output of 'crm' in abook's interface.

Next step is probably to include this in the file ~/bin/crm so I can run '$ crm london' and display the output through abook's interface.

Magic Banana

I am a member!

I am a translator!

Offline
Joined: 07/24/2010

1/ The script does not match 'London'; I need to figure how to ignore case distinction, like with -i in grep.

I guess you can put the whole records on separate lines, by substituting '\n' for an unused string (the \034 character below), use grep, and reformat the output the other way around (I used sed and tr below, but AWK's gsub function would work too):

awk -v RS='' '{ gsub("\n", "\034"); print }' /home/user/.abook/addressbook | grep -i "$*" | sed 's/$/\n/' | tr \\034 \\n

2/ the script returns records for which 'london' isn't necessarily equals to the field 'city'; it would return the record of another field equals to 'london', for example the field 'custom1'.

I do not know the format, but I guess the regular expression can match the field name and the field itself. That said, if you keep on considering more and more complicated queries, you will want to use an actual database engine at some point. Maybe SQLite.

Staircase
Offline
Joined: 02/24/2022

> I guess you can put the whole records on separate lines, by substituting '\n' for an unused string (the \034 character below), use grep, and reformat the output the other way around (I used sed and tr below, but AWK's gsub function would work too) [...]

Thank you. That works. It renders both, lower and upper case keywords. Thanks.

> I am using this now:
> $ crm london > tmp.address && abook --datafile tmp.address
> to display the output of 'crm' in abook's interface.

The shortcomings of rendering the output in abook via a new created, tmp.address, file is that when I edit a contact, only the file tmp.address gets updated, obviously. The original address file /home/user/.abook/addressbook remains the same. I am wondering if there is a possibility to diff-patch those changes from tmp.address to /home/user/.abook/addressbook but I don't know how that would work; or whether that is the right way to think about it. Any suggestions or hints on how to approach this would be welcome.