Created
February 25, 2016 16:34
-
-
Save evillemez/586b92aac0f3a62bf9bd to your computer and use it in GitHub Desktop.
simple script to clean a csv file that has an expected structure
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/env php | |
<?php | |
// Note: this script assumes a CSV structure of: | |
// email,firstName,lastName | |
// | |
// The script dudupes by email and trims all fields. Rows | |
// missing an email are discarded. Validity of email is otherwise | |
// not checked. | |
// | |
// Output is dumped to stdout, so use like so: | |
// | |
// ./clean-csv.php /path/to/input.csv > /path/to/output.csv | |
$raw = file($argv[1]); | |
$map = []; | |
foreach ($raw as $row) { | |
list($email, $firstName, $lastName) = explode(',', $row); | |
if (!$email) continue; | |
if (!$firstName && !$lastName) continue; | |
$map[$email] = [$firstName, $lastName]; | |
} | |
echo PHP_EOL; | |
foreach ($map as $email => $names) { | |
echo implode(',', [ | |
trim($email), | |
trim($names[0]), | |
trim($names[1]) | |
]).PHP_EOL; | |
} | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment