LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 12-06-2012, 08:17 AM   #1
crowzie
LQ Newbie
 
Registered: Jul 2011
Posts: 12

Rep: Reputation: Disabled
convert txt file to vcard


Hi peeps

I got this code to convert an email list to vcf but the fields are wrong along with the format:


Code:
awk -F\t ' {print "BEGIN:VCARD"; print "VERSION:2.1"; print "N:"$1; print "EMAIL;type=WORK;type=pref:"$3; print"TEL;type=WORK;type=pref:"$2; print "END:VCARD" $4; print $4 }' infile.txt > newfile.vcf


The output needs to look like this:

BEGIN:VCARD
VERSION:2.1
N:;addy1@msn.com
FN:addy1@msn.com
EMAIL;PREF;INTERNET:addy1@msn.com
REV:20121206T114903Z
END:VCARD

BEGIN:VCARD
VERSION:2.1
N:;addy2@msn.com
FN:addy2@msn.com
EMAIL;PREF;INTERNET:addy2@msn.com
REV:20121206T114903Z
END:VCARD

And so on

P.s
This list of emails is quite big.

Many thanks in advance.

Last edited by crowzie; 12-07-2012 at 04:17 PM.
 
Old 12-06-2012, 01:37 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
1) please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

2) What does the input file look like? What output do you get with it now? What exactly is wrong with it?

3) At a quick glance, all those separate print commands can certainly be combined into a single print or printf.
 
1 members found this post helpful.
Old 12-09-2012, 08:38 AM   #3
crowzie
LQ Newbie
 
Registered: Jul 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
The input file looks like this

addy1@msn.com
addy2@msn.com
addy3@msn.com

And the output i get at the moment from using the code above is this

BEGIN:VCARD
VERSION:2.1
N:addy1@msn.com

EMAIL;type=WORK;type=pref:
TEL;type=WORK;type=pref:
END:VCARD

BEGIN:VCARD
VERSION:2.1
N:addy2@msn.com

EMAIL;type=WORK;type=pref:
TEL;type=WORK;type=pref:
END:VCARD

BEGIN:VCARD
VERSION:2.1
N:addy3@msn.com

EMAIL;type=WORK;type=pref:
TEL;type=WORK;type=pref:
END:VCARD
 
Old 12-16-2012, 06:05 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
I apologize for being so late getting back to you. I've been rather busy.

As I asked, please use ***[code][/code]*** tags around your code and data.

So all you have is one address per line, with nothing else, correct? Whereas the command you have was certainly designed for input that contained several fields on each line.

You should take a little time to understand how awk works. Each field on the line (or record) is given a separate address, which is referred to by prefixing its number with a "$". So all you really need to do is change the print format so that it uses the same "$1" field for every entry.

But I also still recommend updating it to use printf instead.

Code:
awk '{ printf( "BEGIN:VCARD\nVERSION:2.1\nN:;%s\nFN:%s\nEMAIL;PREF;INTERNET:%s\nREV:20121206T114903Z\nEND:VCARD\n\n" , $1 , $1 , $1 ) }' infile.txt > newfile.vcf
I question the value in the REV line though. It appears to be a date string, and at the very least should probably be unique for each entry. We could easily modify the command to insert the current date, for example:

Code:
awk 'BEGIN{ pfmt=BEGIN:VCARD\nVERSION:2.1\nN:;%s\nFN:%s\nEMAIL;PREF;INTERNET:%s\nREV:%s\nEND:VCARD\n\n" ; dfmt="%Y%m%dT%H%M%SZ" ; } { printf( pfmt , $1 , $1 , $1 , strftime(dfmt) ) }' infile.txt > newfile.vcf
I had to make an educated guess as to the date format. Notice also how I took the opportunity to move the printf formatting string into the BEGIN statement as well, for logic and readability. Finally note that the strftime function is a gawk extension, and so not portable across all systems.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
2 members found this post helpful.
Old 12-22-2012, 08:20 AM   #5
crowzie
LQ Newbie
 
Registered: Jul 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks it worked perfectly.
After doing a long search it appears there are not many txt or csv to vcard applications around as they are quite complicated to make.

Thanks again you are a lifesaver!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 07:23 AM
Copy the contents of a txt file to other txt files (with similar names) by cp command TheIndependentAquarius Linux - Newbie 7 07-03-2010 12:54 AM
Need to convert a large number of file types from none standard to txt metalme Linux - Newbie 2 09-28-2009 05:46 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM


All times are GMT -5. The time now is 03:59 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