reorder matched text file from master textfile

hi there

this script below matches a master file up to the = everything after that is ignored

Problem is the match file is not ordered like the master file.

Is there anyway to output the file exactly in same the order as the master. And save as CSV format.

Master file line by line text file

http://domain1=hdf
http://domain2=fds
http://domain3=rwe
http://www.domain4=231
http://domain5=234
http://www.domain6=342

Match file line by line text file

http://www.domain6=436
http://www.domain4=472
http://domain2=cvf
http://domain3=234

the csv output

Column A                                Column B

http://domain1=hdf                  http://domain1=hdf 
http://domain2=fds                  http://domain2=cvf
http://domain3=rwe                 http://domain3=234
http://www.domain4=231         http://www.domain4=472
http://domain5=234                 http://domain5=234
http://www.domain6=342         http://www.domain6=436

as some urls are not in the second file insert the master url in its place.(it should do that anyways)

Would it also be possible to make sure that each line for each file has no space gap at the end on csv output

http://domain5=234 <----some are like this

http://domain5=234<---- flush to the last character

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = explode("\n",file_get_contents($master_file));
$match_array = explode("\n",file_get_contents($match_file));

foreach($match_array as $match_item) {
 $parse = parse_url($match_item);
 $parse = $parse['scheme']."://".$parse['host']."/";
 $check = false;
 foreach ($master_array as $master_item) {
  if (strstr($master_item,$parse)) {
   $output[] = $match_item;
   $check = true;
  }
 }
 if ($check == false) $output[] = "\n";
}

$output = implode("\n",$output);

$handle = fopen($output_file,"w") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

echo "Analysis Complete!";
?>

500 points for a solution

thanks
playstatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BenMorelCommented:
Hello playstat, this seems to work fine :

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($master_file)));
$match_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($match_file)));
$second_array = array();

// apply trim() to remove blank chars
foreach ($master_array as $key=>$master_item) $master_array[$key] = trim($master_item);
foreach ($match_array as $key=>$match_item) $match_array[$key] = trim($match_item);

// analyse
foreach($master_array as $key=>$master_item) {
 $master_parse = explode('=', $master_item);
 unset($second_item);
 foreach ($match_array as $match_item) {
  $match_parse = explode('=', $match_item);
  if ($master_parse[0] == $match_parse[0]) {
   $second_item = $match_item;
   break;
  }
 }
 $second_array[$key] = isset($second_item) ? $second_item : $master_item;
}

$output = array();
foreach($master_array as $key=>$master_item) {
 $output[] = "{$master_item};{$second_array[$key]}";
}
$output = implode("\n", $output);

$handle = fopen($output_file,"wb") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

echo "Analysis Complete!";
?>

Regards,
Ben
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
suresh_aspCommented:
Try this,

the output file.csv will be inorder to master file with respect to matches

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = explode("\n",file_get_contents($master_file));
$match_array = explode("\n",file_get_contents($match_file));

foreach($master_array as $master_item) {
  $check = false;

 foreach ($match_array as $match_item) {
  if (strcmp(substr($master_item,0,strpos($master_item,"=")+1),substr($match_item,0,strpos($match_item,"=")+1)) == 0) {
   $output[] = $master_item;
   $check = true;
  }
 }
}

$output = implode("\n",$output);

$handle = fopen($output_file,"w") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

echo "Analysis Complete!";
?>
0
playstatAuthor Commented:
Ben that is perfect great !

Can you add one more thing would it be possible to output a third file a text file line by line that shows all urls with no matches

then you can have the 500 points

plus theres a spin off question if you want to answer it here

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21185631.html

thats for another 500 points

thanks
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

suresh_aspCommented:
Try this,
It will create both matched and nonmatched files

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = explode("\n",file_get_contents($master_file));
$match_array = explode("\n",file_get_contents($match_file));
$check = false;

foreach($master_array as $master_item) {
       $check = false;
 foreach ($match_array as $match_item) {
  if (strcmp(substr($master_item,0,strpos($master_item,"=")+1),substr($match_item,0,strpos($match_item,"=")+1)) == 0) {
   $output[] = $master_item;
   $check = true;
  }
 }
 if ($check == false)
       $nomatches[] = $master_item;
}

$output = implode("\n",$output);
$nomatches = implode("\n",$nomatches);

$handle = fopen($output_file,"w") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

$handle = fopen("nomatches.txt","w+") or die("Cannot open file");
fwrite($handle, $nomatches) or die("Cannot write to file");
fclose($handle);


echo "Analysis Complete!";
?>
0
playstatAuthor Commented:
can you outout the non and matched to 2 serperate text files line by line output if poss

thanks suresh
0
playstatAuthor Commented:
can you include that extra with bens code
0
suresh_aspCommented:
Changed with respect to bens code:
creates two files with matched and nonmatches ones

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($master_file)));
$match_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($match_file)));
$second_array = array();

// apply trim() to remove blank chars
foreach ($master_array as $key=>$master_item) $master_array[$key] = trim($master_item);
foreach ($match_array as $key=>$match_item) $match_array[$key] = trim($match_item);

// analyse
foreach($master_array as $key=>$master_item) {
 $check = false;
 $master_parse = explode('=', $master_item);
 unset($second_item);
 foreach ($match_array as $match_item) {
  $match_parse = explode('=', $match_item);
  if ($master_parse[0] == $match_parse[0]) {
   $second_item = $match_item;
   $check = true;
   break;
  }
 }
 $second_array[$key] = isset($second_item) ? $second_item : $master_item;
 if ($check == false)
       $non_match[$key] = $master_item;
}

$output = array();
foreach($master_array as $key=>$master_item) {
 $output[] = "{$master_item};{$second_array[$key]}";
}

$output = implode("\n", $output);

$handle = fopen($output_file,"wb") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

$non_match = implode("\n", $non_match);
$handle = fopen("nomatches.txt","w+") or die("Cannot open file");
fwrite($handle, $non_match) or die("Cannot write to file");
fclose($handle);

echo "Analysis Complete!";
?>

Regards
suresh
0
playstatAuthor Commented:
is it possible to add the nonmatches of the match file to column C
0
suresh_aspCommented:
Now you will get nonmatches to column C

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($master_file)));
$match_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($match_file)));
$second_array = array();

// apply trim() to remove blank chars
foreach ($master_array as $key=>$master_item) $master_array[$key] = trim($master_item);
foreach ($match_array as $key=>$match_item) $match_array[$key] = trim($match_item);

// analyse
foreach($master_array as $key=>$master_item) {
 $check = false;
 $master_parse = explode('=', $master_item);
 unset($second_item);
 foreach ($match_array as $match_item) {
  $match_parse = explode('=', $match_item);
  if ($master_parse[0] == $match_parse[0]) {
   $second_item = $match_item;
   $check = true;
   break;
  }
 }
 $second_array[$key] = isset($second_item) ? $second_item : $master_item;
 if ($check == false)
       $non_match[$key] = $master_item;
}

$output = array();
foreach($master_array as $key=>$master_item) {
 $output[] = "{$master_item}\t\t\t\t{$second_array[$key]}\t\t\t\t{$non_match[$key]}";
}

$output = implode("\n", $output);

$handle = fopen($output_file,"wb") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

$non_match = implode("\n", $non_match);
$handle = fopen("nomatches.txt","w+") or die("Cannot open file");
fwrite($handle, $non_match) or die("Cannot write to file");
fclose($handle);

echo "Analysis Complete!";
?>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.