LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 11-03-2006, 03:49 AM   #1
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 635

Rep: Reputation: 35
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
 
Old 11-03-2006, 04:02 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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
 
Old 11-03-2006, 04:39 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 644

Rep: Reputation: 153Reputation: 153
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
 
Old 11-03-2006, 05:26 AM   #4
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 635

Original Poster
Rep: Reputation: 35
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
 
Old 11-03-2006, 06:47 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 11-05-2006, 02:33 PM   #6
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 635

Original Poster
Rep: Reputation: 35
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
 
Old 11-05-2006, 02:51 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM
Get Library Version Using AWK allomeen Linux - General 4 05-02-2006 12:13 PM
LXer: CLI Magic: Learn to talk awk LXer Syndicated Linux News 0 01-16-2006 04:31 AM
awk cli to rename a list of files... pld Linux - General 4 02-15-2005 10:57 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM


All times are GMT -5. The time now is 03:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration