Skip to content

Commit

Permalink
Add remove_from_file.php
Browse files Browse the repository at this point in the history
  • Loading branch information
matks committed Feb 18, 2016
1 parent b3f2598 commit 7722676
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ php explore_url.php <url> [--urldecode]
Explode an URL in different parts to ease reading

explore_sql_query
-----------
-----------------

```bash
php explore_sql_query.php <query>
```
Display a SQL query formatted in a more readable way

compare_sql_tables
-----------
------------------

```bash
php compare_sql_tables.php
```
Compare two SQL tables to check whether their content is identical

Add settings/credentials in a config.php file to run it

remove_from_file
------------------

```bash
php remove_from_file.php file1 file2 output
```
Compare two text files and remove from file1 the lines from file2
4 changes: 3 additions & 1 deletion Util/TextColorWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TextColorWriter
const BASH_PROMPT_GREEN = 32;
const BASH_PROMPT_YELLOW = 33;
const BASH_PROMPT_BLUE = 34;
const BASH_PROMPT_CYAN = 36;
const BASH_PROMPT_WHITE = 37;

/**
Expand Down Expand Up @@ -49,7 +50,8 @@ private function getKnownColors()
static::BASH_PROMPT_GREEN,
static::BASH_PROMPT_YELLOW,
static::BASH_PROMPT_BLUE,
static::BASH_PROMPT_WHITE
static::BASH_PROMPT_WHITE,
static::BASH_PROMPT_CYAN,
);

return $colors;
Expand Down
94 changes: 94 additions & 0 deletions remove_from_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php


/**
* Validate console input
*
* Die if input is not valid
*
* @param array $argv user inputs
*/
function validateInput($argv)
{
if (count($argv) !== 4) {
echo 'php remove_from_file <file1> <file2> <output>' . PHP_EOL;
die(1);
}

if (!file_exists($argv[1])) {
echo "Error: " . $argv[1] . " is not a valid file" . PHP_EOL;
die(1);
}
if (!file_exists($argv[2])) {
echo "Error: " . $argv[2] . " is not a valid file" . PHP_EOL;
die(1);
}
}

/**
* @param string $filepath
*
* @return array blacklist is in keys
* @throws Exception
*/
function computeBlackListFromFile($filepath)
{
$handle = fopen($filepath, "r");

if (false === $handle) {
throw new \Exception("Could not open file $filepath");
}

$blackList = array();

while (($line = fgets($handle)) !== false) {
$blackList[trim($line)] = true;
}

fclose($handle);

return $blackList;
}

validateInput($argv);

require 'Util/TextColorWriter.php';

$filepath1 = $argv[1];
$filepath2 = $argv[2];
$outputFilepath = $argv[3];

$blackList = computeBlackListFromFile($filepath2);

$fileHandle1 = fopen($filepath1, "r");
$outputFile = fopen($outputFilepath, 'w');

if (false === $fileHandle1) {
throw new \Exception("Could not open file $fileHandle1");
}
if (false === $outputFile) {
throw new \Exception("Could not write file $outputFile");
}

$i = 0;
$removed = 0;
while (($line = fgets($fileHandle1)) !== false) {

$isNotInBlacklist = (false === isset($blackList[trim($line)]));

if ($isNotInBlacklist) {
fwrite($outputFile, $line);
} else {
$removed++;
}

$i++;
}

fclose($fileHandle1);
fclose($outputFile);

// echo result
echo TextColorWriter::textColor('Done', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
echo TextColorWriter::textColor("Processed : $i lines", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL;
echo TextColorWriter::textColor("Removed : $removed lines", TextColorWriter::BASH_PROMPT_CYAN) . PHP_EOL;

0 comments on commit 7722676

Please sign in to comment.