Sunday, October 30, 2016

CSV maker C++

I hastily put this program together to add contacts to a gmail account from a text file. I have school papers and books and I want to scrape those for email addresses and save them to a text file. After that I'll port the text file to a correctly formatted CSV file so I can import the email address list into my contacts then add people on facebook. First I'll use grep to get the email addresses from all of the documents.

grep -hrio "\b[a-z0-9.-]\+@[a-z0-9.-]\+\.[a-z]\{2,4\}\+\b" /root/Desktop/docs/ > emails.txt
Next I'll sort them for uniqueness.

 sort -i emails.txt | uniq -u > emails1.txt

Next I'll make sure there no .gov or .mil addresses. I'm only adding people with college email addresses or commercial email address. You can apply further expressions to the earlier grep statement and just get emails from certain domains if you want.

sed -i '/.gov/d' emails1.txt
sed -i '/.mil/d' emails1.txt
 Now I'll count the lines in my text file and see if I'm over 5,000 contacts. gmail will not let you import more than 5,0000 at a time. There's a few ways to get a line count.

sed -n '$=' emails1.txt
 Or

cat emails1.txt | wc -l

If the count is more than 5,000 lines I'll have to split the emails list up into smallers files.

split --suffix-length=8 --lines=2500 emails1.txt

Now I'll run this hastily coded C++ program

 //application to sort email addresses and put them into a CSV for use with gmail.
#include <string>
#include <sstream>
#include <iostream>
#include <cctype>
#include <fstream>
#include <iomanip>
using namespace std;

int main (int argc, char* argv[])
{

//build command line statement from string variables

{ofstream myfile;
myfile.open ("/root/Desktop/emails.csv");

{ string line; ifstream infile ("/root/file.txt");

//input the layout information for CSV file
myfile << "Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Group Membership,E-mail 1 - Type,E-mail 1 - Value,E-mail 2 - Type,E-mail 2 - Value" << endl;

if (infile.is_open())

{ while ( getline (infile,line) )
//output conacts to contact list. Comas delimit contact information fields.
myfile << ",,,,,,,,,,,,,,,,,,,,,,,,,,,* ," << line << ",," << endl;

infile.close();

myfile.close();
} else cout << "Unable to open file" << endl;
}
}

return 0; }

Now you can import your CSV into gmail and add your the email addresses to your contact list. Then you can invite friends on facebook from your gmail account. Even if you don't actually know the people facebook will invite them because you have their email addresses. You can also do this with Yahoo! and other email services but the CSV file formats may be a little different. It's mostly a matter of getting the header information correct and having the correct number of commas.

2 comments: