A
analogquack
I am needing to copy a line of text within a text file and insert a
slightly modified version of that line at a different point in same
file. I have had difficulty finding any text I can grok which would
assist me in this matter.
If you don't care about the details of my specific project, any general
example would be very helpful and gratefully accepted. If you do care
about the details (or if it helps someone else who is trying to perform
the same task) the specifics of my challenge and the script I am
working with follow.
The goal is to add N: and FN: entries into vCards having none by
copying the data from the ORG: line. In English, some contacts have
company names but no first and last name, which are represented on the
N: and FN: lines. Some address book apps expect to have a line in the
N: or FN: fields (Outlook for example) while others don't care (Palm
Desktop for example).
To be specific, the text files look like this:
BEGIN:VCARD
VERSION:2.1
N:LastName;FirstName
FN:FirstName LastName
TITLE:Title
ORG:Company
ADR;WORK:;;WorkAddress;WorkCity;WorkState;WorkZip;WorkCountry
URL;WORK:Website
TEL;WORK:Work#
When there is no N: or FN: fields, they would look like so:
BEGIN:VCARD
VERSION:2.1
TITLE:Title
ORG:Company
ADR;WORK:;;WorkAddress;WorkCity;WorkState;WorkZip;WorkCountry
URL;WORK:Website
TEL;WORK:Work#
I need to copy the ORG: line, "ORG:Company" in this example, and have
it inserted just below the VERSION:2.1 line like so:
BEGIN:VCARD
VERSION:2.1
N:Company
FN:Company
TITLE:Title
ORG:Company
ADR;WORK:;;WorkAddress;WorkCity;WorkState;WorkZip;WorkCountry
URL;WORK:Website
TEL;WORK:Work#
I would like to integrate this into a Perl script I have found in a
prior thread which works well except for this one lacking feature. The
script parses a single Palm Desktop-exported vCard file having multiple
contacts within it and output separate vCard files, one for each
contact. If I could integrate the two functions I would be able to
transform my thousand or so contacts in one fell swoop into files that
can be imported into Outlook. =)
Here is the script as I am currently using it:
#!/usr/bin/perl -w
use strict;
# vcard 2.1 - rfc2425,rfc2426
my $pathname = 'C:/No_Install/_Analog/Office/Output';
my $sourcefilename = '../Palm.vcf';
$/ = ''; # set paragraph mode
open SOURCE, "< $pathname/$sourcefilename"
or die "Couldn't open $sourcefilename for reading: $!";
while ( <SOURCE> ) {
chomp;
my $sinkfilename;
if ( /^(fn[;:].+)/im ) {
( undef, $sinkfilename ) = split /(?<!\\):/, $1, 2;
}
elsif ( /^(n[;:].+)/im ) {
( undef, $sinkfilename ) = split /(?<!\\):/, $1, 2;
# n: field is "lastname;firstname"
# change to "firstname lastname"
$sinkfilename = join ' ', reverse split /(?<!\\);/,
$sinkfilename;
}
$sinkfilename =~ s/[^\w~,\- ]//g;
my $count = '';
if ( -e "$pathname/$sinkfilename" ) {
1 while -e "$pathname/" . ++$count . "$sinkfilename" . ".vcf";
}
$sinkfilename .= ".vcf" ;
open SINK, "> $pathname/$count$sinkfilename"
or die "Couldn't open $count$sinkfilename for writing: $!";
print SINK "$_\n" or die "can't write $count$sinkfilename: $!";
close SINK or die "couldn't close $count$sinkfilename: $!";
}
close SOURCE or die "couldn't close $sourcefilename: $!";
__END__
You can see the thread I gaffed this from here for reference:
http://groups.google.com/group/comp...st&q=split+palm+vcard&rnum=1#79e269b866d12028
slightly modified version of that line at a different point in same
file. I have had difficulty finding any text I can grok which would
assist me in this matter.
If you don't care about the details of my specific project, any general
example would be very helpful and gratefully accepted. If you do care
about the details (or if it helps someone else who is trying to perform
the same task) the specifics of my challenge and the script I am
working with follow.
The goal is to add N: and FN: entries into vCards having none by
copying the data from the ORG: line. In English, some contacts have
company names but no first and last name, which are represented on the
N: and FN: lines. Some address book apps expect to have a line in the
N: or FN: fields (Outlook for example) while others don't care (Palm
Desktop for example).
To be specific, the text files look like this:
BEGIN:VCARD
VERSION:2.1
N:LastName;FirstName
FN:FirstName LastName
TITLE:Title
ORG:Company
ADR;WORK:;;WorkAddress;WorkCity;WorkState;WorkZip;WorkCountry
URL;WORK:Website
TEL;WORK:Work#
When there is no N: or FN: fields, they would look like so:
BEGIN:VCARD
VERSION:2.1
TITLE:Title
ORG:Company
ADR;WORK:;;WorkAddress;WorkCity;WorkState;WorkZip;WorkCountry
URL;WORK:Website
TEL;WORK:Work#
I need to copy the ORG: line, "ORG:Company" in this example, and have
it inserted just below the VERSION:2.1 line like so:
BEGIN:VCARD
VERSION:2.1
N:Company
FN:Company
TITLE:Title
ORG:Company
ADR;WORK:;;WorkAddress;WorkCity;WorkState;WorkZip;WorkCountry
URL;WORK:Website
TEL;WORK:Work#
I would like to integrate this into a Perl script I have found in a
prior thread which works well except for this one lacking feature. The
script parses a single Palm Desktop-exported vCard file having multiple
contacts within it and output separate vCard files, one for each
contact. If I could integrate the two functions I would be able to
transform my thousand or so contacts in one fell swoop into files that
can be imported into Outlook. =)
Here is the script as I am currently using it:
#!/usr/bin/perl -w
use strict;
# vcard 2.1 - rfc2425,rfc2426
my $pathname = 'C:/No_Install/_Analog/Office/Output';
my $sourcefilename = '../Palm.vcf';
$/ = ''; # set paragraph mode
open SOURCE, "< $pathname/$sourcefilename"
or die "Couldn't open $sourcefilename for reading: $!";
while ( <SOURCE> ) {
chomp;
my $sinkfilename;
if ( /^(fn[;:].+)/im ) {
( undef, $sinkfilename ) = split /(?<!\\):/, $1, 2;
}
elsif ( /^(n[;:].+)/im ) {
( undef, $sinkfilename ) = split /(?<!\\):/, $1, 2;
# n: field is "lastname;firstname"
# change to "firstname lastname"
$sinkfilename = join ' ', reverse split /(?<!\\);/,
$sinkfilename;
}
$sinkfilename =~ s/[^\w~,\- ]//g;
my $count = '';
if ( -e "$pathname/$sinkfilename" ) {
1 while -e "$pathname/" . ++$count . "$sinkfilename" . ".vcf";
}
$sinkfilename .= ".vcf" ;
open SINK, "> $pathname/$count$sinkfilename"
or die "Couldn't open $count$sinkfilename for writing: $!";
print SINK "$_\n" or die "can't write $count$sinkfilename: $!";
close SINK or die "couldn't close $count$sinkfilename: $!";
}
close SOURCE or die "couldn't close $sourcefilename: $!";
__END__
You can see the thread I gaffed this from here for reference:
http://groups.google.com/group/comp...st&q=split+palm+vcard&rnum=1#79e269b866d12028