Created
December 12, 2010 21:05
-
-
Save sriranggd/738325 to your computer and use it in GitHub Desktop.
A Nodejs script to extract name and phone numbers (only) from a bunch of VCF files into a csv file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
/** | |
* Author : Srirang (Brahmana) | |
* Blog : http://techbrahmana.blogspot.com/ | |
* Site : http://srirang.co.cc/ | |
* | |
* As it is evident this is an absolutely crude script (The output file path is hardcoded... !!!). | |
* But this works. I have tried this on the VCF files generated by a Nokia 3600 Slide backup operation. | |
* This script only extracts the Name and the phone numbers. I do not know the actual VCF spec. I just looked at a | |
* few VCF files and figured out the N line (for name) and TEL line (for phone numbers). It pulls out every number | |
* but not the type - like Work, Cellphone, etc.. | |
* | |
* Usage : $>node <path-to-this-script> <path-to-vcf-file(s)> | |
* If you have multiple files, just use the shell wild characters (*) like this : | |
* | |
* $>node <path-to-this-script> /directory-containing-vcf-files/*.vcf | |
* | |
* I don' think this script is ever going to be used by anyone else, but still just to be on the safer side I am | |
* including this licensing thing here. It's GPLv3 or later. | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
var fs = require('fs'); | |
function vcfToCsv(vcfStr) { | |
// Split and those stuff | |
var lines = vcfStr.split('\r\n'); | |
var csvStr = ''; | |
var name = '' | |
var numbers = []; | |
for(var l in lines) { | |
line = lines[l]; | |
if (line[0] == 'N') { | |
var parts = line.split(':'); | |
name = parts[parts.length - 1]; | |
} | |
if (line.indexOf('TEL') == 0) { | |
var parts = line.split(':'); | |
var num = parts[parts.length - 1]; | |
numbers.push(num); | |
} | |
} | |
csvStr += name; | |
for (var n in numbers) { | |
csvStr += ',' + numbers[n]; | |
} | |
csvStr += '\n'; | |
return csvStr; | |
} | |
vcfList = []; | |
csvList = ''; | |
args = process.argv; | |
synError = false; | |
vcfStartIndex = 2; | |
filesDone = 0; | |
if (args.length < 3) | |
{ | |
if (args.length == 2 && args[0] == "node") | |
synError = true; | |
else | |
vcfStartIndex = 1; | |
} | |
if (synError) { | |
console.log("Usage : \nnode <path-to-this-script> <path-to-vcf-file(s)>\nor\n<path-to-this-script> <path-to-vcf-file(s)"); | |
return; | |
} | |
vcfList = args.slice(vcfStartIndex); | |
vcfList.forEach(function(vcfFileName) { | |
fs.readFile(vcfFileName, | |
function(err, data) { | |
if (err) | |
console.log(err.message); | |
csvStr = vcfToCsv(data.toString()); | |
csvList += csvStr; | |
filesDone++; | |
}); | |
}); | |
console.log("Processing : "); | |
function sleep() { | |
setTimeout(writeCsv, 2000); | |
} | |
function writeCsv() { | |
if (filesDone < vcfList.length) { | |
console.log("."); | |
sleep(); | |
} | |
else { | |
fs.writeFile('/path/to/output.csv', csvList.toString(), | |
function(err) { | |
if (err) | |
console.log(err.message); | |
else | |
console.log("CSV Saved\n"); | |
}); | |
} | |
} | |
sleep(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment