LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   convert txt file to vcard (https://www.linuxquestions.org/questions/linux-newbie-8/convert-txt-file-to-vcard-4175440265/)

crowzie 12-06-2012 08:17 AM

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.

David the H. 12-06-2012 01:37 PM

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.

crowzie 12-09-2012 08:38 AM

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

David the H. 12-16-2012 06:05 AM

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/

crowzie 12-22-2012 08:20 AM

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!


All times are GMT -5. The time now is 05:11 PM.