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. |
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. |
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 |
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 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 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/ |
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. |