Results 1 to 6 of 6
and I'd like to do it scripted, since there's about a hundred of them all with the same issue.
The Problem:
KAddressBook and probably a bunch of other phone book ...
- 10-03-2009 #1Linux Guru
- Join Date
- Jan 2009
- Location
- Dover, NH
- Posts
- 1,633
I need to reformat some text files...
and I'd like to do it scripted, since there's about a hundred of them all with the same issue.
The Problem:
KAddressBook and probably a bunch of other phone book software when asked to export to a vcard 2.1 vcf file, outputs something I'm assuming is typically generic and okay to spec... but my Samsung phone rejects it. I need to make several edits to each vcard to turn it in to something I can import to my phone.
The KAddressBook output:
What the phone expects:Code:BEGIN:VCARD FN:NH SPCA (Stratham) N:;;;; ORG:NH SPCA (Stratham) TEL;TYPE=WORK:603 772 2404 UID:7oESBBbuvk VERSION:2.1 END:VCARD
The list of edits is as follows:Code:BEGIN:VCARD VERSION:2.1 FN:NH SPCA (Stratham) N:;;;; ORG:NH SPCA (Stratham) TEL;WORK:6037722404 END:VCARD
*Relocate the line "VERSION:2.1" from next before last line to line 2
*Find the line(s) that start with "TEL", remove "TYPE=" and remove any blank space between numbers
- The cards may have up to eight such lines in each, though most only have 1-3
*Find and delete the line beginning with "UID".
I note in the vcards that each line ends with 0D before 0A, so some care must be taken to not simply write a line but to actually perform the cut-n-past action when moving the "VERSION" line (discovered while editing in mc). Other than that these are just plain text files. Any suggestions?
Thanks.
If I or anyone could get this compatibility script written, that'd be totally awesome for all... I can't imagine I'm the only one who's run into this problem; such a script could probably help hundreds of Samsung phone owners.
(BTW: Mine's a SPH-M540, aka Rant aka Slyde; glorious lack of support for anything other than a basic modem. )
- 10-03-2009 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
sed '/UID/d;/TEL/s/TYPE=//;s/ //g;/VERSION/d;/BEGIN/a\VERSION 2.1\r' <filename
perhaps the "\r" suffices for the "0D" and "0A" issue--perhaps not
Last edited by tpl; 10-03-2009 at 07:06 AM.
the sun is new every day (heraclitus)
- 10-03-2009 #3Linux Guru
- Join Date
- Jan 2009
- Location
- Dover, NH
- Posts
- 1,633
I had to make a slight edit...
sed '/UID/d;/TEL/s/TYPE=//;s/ //g;/VERSION/d;/BEGIN/a\VERSION:2.1\r' <
The ":" matters. Hehe, otherwise it seems to work... and now I'm finding out some other limitations of the phone.
Some cards have a line that starts with "ROLE:" that is ignored by the phone, i need to replace it with "NOTE:"
Next I'm also finding that it ignores the Formatted Name field (FN:), and only places in the name field (N:), and KAdressBook has a fondness for outputting Formatted Names. The phone will accept the cards, but place the entries at the end under "No Name."
The easiest way I can think of doing this is to delete the existing N: line, replace FN: with just N:, and add the four obligatory ";;;;" afterwards (followed by the \r, or can the semicolons be inserted?).
I'll work this and see if I can solve it. Thanks for the excellent foothold.!
- 10-03-2009 #4Linux Guru
- Join Date
- Jan 2009
- Location
- Dover, NH
- Posts
- 1,633
I think I have it... time for some testing, but this seems like it'll make the grade at the moment. I'm mainly posting right now just to log my progress.
Next I'll tackle a for loop so I can input a wildcard for all files in a directory and output reformatted files in a different location (thus one command to fix all files).
Anyway, this what I have right now for sed:
sed '/UID/d;/TEL/s/TYPE=//;/TEL/s/ //g;/ROLE/s/ROLE/NOTE/;/^N:/d;/FN:/s/FN:/N:/;/^N:/s/\r/\;\;\;\;\r/;/VERSION/d;/BEGIN/a\VERSION:2.1\r' < input_fileLast edited by D-cat; 10-03-2009 at 11:02 PM.
- 10-04-2009 #5Linux Guru
- Join Date
- Jan 2009
- Location
- Dover, NH
- Posts
- 1,633
Half-@55 Solved.
It works for me. If anyone wants to make it more politically correct, go for it.
I did it in terms of two scripts. The first one does the work. It accepts one argument- the filename to be processed. First it creates a subdirectory vcfix so the output doesn't step on the input. Then it calls the sed script to make the necessary alterations so this stupid phone will accept the card. The converted output file is the same filename as the input but placed in the subdir.
The second script simply calls find to execute vcfix on all files in the directory.
ISSUES:
Well, this first one is ugly; it should probably do several tests, such as checking for the existence of the subdir vcfix before just blindly trying to create it, and testing for the existence of the filename (or even an argument period) so it can fail out gracefully of there is an error, maybe with instructions. I suck at it and have other things to do with my weekend (sorry).
The other is that both commands expect to be run from the directory of the source file(s). vcfixall also assumes that all files in the directory are *.vcf files and makes no attempt to test otherwise. I have no idea what will happen if either condition is not true, I haven't tested it.
Otherwise, here's my half-baked solution. It worked for me; all the contacts I wanted to transfer into the Samsung phone worked as desired. Use or modify to your needs and desire.
(Also... just to rant about the Rant phone itself... I tried the source vcf's (the ones KAddressBook outputs) on a different phone... a LG brand NET10 phone, and it worked fine as-is. This tells me that Samsung ought to loosen up the requirements a little and improve the vcard parsing for compatibility.)
vcfix
usage: vcfix [filename]
vcfixallCode:#!/bin/sh mkdir vcfix 2> /dev/null sed '/UID/d;/TEL/s/TYPE=//;/TEL/s/ //g;/ROLE/s/ROLE/NOTE/;/^N:/d;/FN:/s/FN:/N:/;/^N:/s/\r/\;\;\;\;\r/;/VERSION/d;/BEGIN/a\VERSION:2.1\r' < $1 > vcfix/$1
Code:#!/bin/sh find ./ -maxdepth 1 -type f -exec vcfix '{}' \;
- 10-08-2009 #6Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
very nice
the sun is new every day (heraclitus)


Reply With Quote