The problem with the second example you posted, is there is almost no structure to it. It doesn't even have a consistent ordering of rows for each record.
I think this is where Perl shines, so I went ahead and wrote up a prototype. ( took me about an hour )
I don't think there exists a tool that would be any simpler, that would get as decent a result. ( There might be a tool that would be easier to learn, but it still won't be any simpler to setup )
All it does is parse the input file, and give a computer readable output.
You would still have to do some hand editing of either the input or the output, but it does a fairly decent job on your sample.
The code was designed to not lose a single piece of information, so it would still need some processing in order to load it into a database.
It just throws whatever it can't decipher into comment
.
If you knew a line should be tagged as hours
(for example) you could just add "hours:
" to the beginning of that line.
#! perl
use strict;
use warnings;
use 5.14.0;
# there was some wide chars in the input
use open ':encoding(utf8)';
use open (':std', ':encoding(utf8)');
$/ = "\n\n"; # input record separator
my @data;
RECORD: while( my $record = <> ){
# split on newlines, ignore empty ones
my @lines = grep { length } split /\r?\n/, $record;
my %record = (
name => shift @lines, # first line is the name
);
LINE: for( @lines ){ # for local $_ ( @lines ){...}
s/^\s|\s+$//g; # remove leading and trailing spaces
if( /^(\d+)\.$/a ){ # "22." "23." etc
$record{number} = ''.$1;
}elsif( /^\d{3}.\d{3}.[^\s]{4}/a ){ # loosely looks like a phone number
push @{$record{phone}}, $_; # may be more than one phone number so put it in an array
}elsif( /^(\w+):\s+(.*)/ ){
push @{$record{lc $1}}, ''.$2; # handles website: ...
}elsif( /\d+ .*? \b(?:st(?:reet)?|blvd|boul[ae]vard|ave(?:nue)?)\b/axi ){ # looks like a street address
push @{$record{address}}, $_;
}elsif( /,\s+\w\w\s+\d{5}/a ){ # looks like "city, state, zipcode"
push @{$record{address}}, $_;
}else{
push @{ $record{comment} }, $_; # unknown line
}
}
push @data, \%record;
}
use JSON;
# UTF8 is handled by "use open ..." above
# so we turn it off here
my $encode = JSON->new->utf8(0)->pretty;
print $encode->encode(\@data);
print "\n";
[
{
"website" : [
"www.mariateresasbabies.com"
],
"comment" : [
"Maria Teresa Desaba, Owner/Director; Tony Saba, Org. Director.",
"Serving children 6 wks to 5yrs full-time."
],
"number" : "22",
"name" : "Maria Teresa’s Babies Early Enrichment Center/Daycare ",
"address" : [
"825 23rd Street South",
"Arlington, VA 22202"
],
"phone" : [
"703-979-BABY (2229)"
]
},
{
"website" : [
"www.brighthorizons.com 112 children, ages 6 wks - 5 yrs."
],
"comment" : [
"7:00 a.m. – 6:00 p.m. Summer Camp for children 5 - 9 years."
],
"number" : "23",
"name" : "National Science Foundation Child Development Center ",
"address" : [
"4201 Wilson Blvd., Suite 180 22203"
],
"phone" : [
"703-292-4794"
]
}
]