Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-03-2006, 03:49 AM
|
#1
|
Member
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641
Rep:
|
Using awk to print CLI-version of kaddressbook ?
Hi,
I recently read latest article on Linux.com on "CLI Magic: Command-line contact management" and am trying to make the example there work for a CLI-based reader of KDE's addressbook.
Currently I can grep the ~/.kde/share/apps/kabc/std.vcf, but is it possible to use awk to grep for a name or email address, and then have it print the entire record between the tags BEGIN:VCARD and END:VCARD ?
I tried this without the greatest success:
Code:
awk 'BEGIN { RS = "VCARD" } /${SEARCHNAME}/' std.vcf
It's a start, but I'm a bit stuck in getting further, ie making it print:
Name: firstname lastname
Email: emailaddress
Phone: 1234
Address: somestreet, etc etc
Birthday: ssss
Or does such an awk-script already exist under some obscure non-Googleable name?
-Y1
|
|
|
11-03-2006, 04:02 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
I'm not familiar with the layout of these vcf files. Could you post an example (2 or 3 records are enough)? Would possibly make it a lot easier to help you
|
|
|
11-03-2006, 04:39 AM
|
#3
|
Member
Registered: Jul 2004
Location: Chennai, India
Posts: 952
|
Can u try the following pseudo-code? (A possible algorithm). Assumes that the input data format is text (not having ascii > 127) and on knwn format.
(1) sequentially read the data file until EOF.
(2) If BEGIN:VCARD, then append the line to a variable.
(3) If email or name matches, then set a flag.
(4) If END:VCARD, examine the flag and if on (search criteria was matched), then dump the array. Initialise the array, reset the flag and loop back to step 1.
(5) on EOF, examine the flag and if on, dump the array.
Steps 2, 3 4 are done for each record.
End
|
|
|
11-03-2006, 05:26 AM
|
#4
|
Member
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641
Original Poster
Rep:
|
Quote:
Originally Posted by druuna
I'm not familiar with the layout of these vcf files. Could you post an example (2 or 3 records are enough)? Would possibly make it a lot easier to help you
|
Here comes:
~>less .kde/share/apps/kabc/std.vcf
Code:
BEGIN:VCARD
ADR;TYPE=work:;;P.O. Box 11111;Dubai;;;United Arab Emirates
CATEGORIES:Personal
CLASS:PUBLIC
EMAIL:email@domain.com
FN:Donald Duck
N:Duck;Donald;;;
ORG:Duckster, Inc.
REV:2005-11-01T15:31:08Z
TEL;TYPE=WORK:+971 1 333 5555
TEL;TYPE=FAX:+971 4 330 5555
TEL;TYPE=CELL:+971 5 333 6666
TEL;TYPE=FAX;TYPE=WORK:+971 3 333 3456
UID:0CdjO4uH12gO7
VERSION:3.0
X-KPILOT-RecordID:1061634854
END:VCARD
BEGIN:VCARD
ADR;TYPE=work:;;;Paris;;0111 ;France
ADR;TYPE=home:;;Private Street 5;Paris;;0111;France
BDAY:1971-02-01T00:00:00Z
CATEGORIES:Personal
CLASS:PUBLIC
EMAIL:daffy@duck.com
FN:Daffy Duck
N:Duck;Daffy Mohamed;;;
ORG:Ducksters Son&Co
REV:2001-11-11T15:31:13Z
TEL;TYPE=WORK:+1 111 1111 1111
TEL;TYPE=CELL:+2 222 2222 2222
UID:0dp98Yt75An46g
VERSION:3.0
X-KPILOT-RecordID:106173131323
END:VCARD
BEGIN:VCARD
.....etc...
END:VCARD
|
|
|
11-03-2006, 06:47 AM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
I wrote a general paragraph grep script a while ago:
Code:
#!/bin/bash
# usage: rgrep <string> <file>
# paragraph's should be seperated by a blank line.
pattern=$1
shift
awk -v pattern="$pattern" \
'BEGIN { FS="\n" ; RS=""; ORS="\n\n" }
$0 ~ pattern { print $0 }
' $*
This will also return the BEGIN and END tags, which could be filtered out later.
Sample run:
Code:
$ rgrep "Donald Duck" infile
BEGIN:VCARD
ADR;TYPE=work:;;P.O. Box 11111;Dubai;;;United Arab Emirates
CATEGORIES:Personal
CLASS:PUBLIC
EMAIL:email@domain.com
FN:Donald Duck
N:Duck;Donald;;;
ORG:Duckster, Inc.
REV:2005-11-01T15:31:08Z
TEL;TYPE=WORK:+971 1 333 5555
TEL;TYPE=FAX:+971 4 330 5555
TEL;TYPE=CELL:+971 5 333 6666
TEL;TYPE=FAX;TYPE=WORK:+971 3 333 3456
UID:0CdjO4uH12gO7
VERSION:3.0
X-KPILOT-RecordID:1061634854
END:VCARD
Hope this is good enough.
[edit]
fixed a mistake in the above script
[/edit]
Last edited by druuna; 11-05-2006 at 02:49 PM.
|
|
|
11-05-2006, 02:33 PM
|
#6
|
Member
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641
Original Poster
Rep:
|
Thanks much!
Don't know what I did wrong, because the script simply prints all the records, quite similar to doing cat on the file..
-Y1
|
|
|
11-05-2006, 02:51 PM
|
#7
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
I did find a mistake in my script. This: /bin/awk should be awk. I already changed it in the previous post.
But that does not explain the cat like behavior. Did you copy/paste my code or typed it by hand? Seems to be working here after I copy/pasted it.
|
|
|
All times are GMT -5. The time now is 01:52 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|