Jump to content

[SOLVED] search a line in a txt file


homer.favenir

Recommended Posts

hi to all.

how can i compare 2 txt files, and extract the difference.

e.g.

txt1 - txt2 = txt3,

if txt1 = txt2 then txt3 = 0.

 

txt1 is the needle and txt2 is the haystack.

 

my script is

<?php
$currFile = file("Portfolio extracted/sent/UFPB Catalog_5-30-08.txt");
$prevFile = file("Portfolio extracted/sent/UFPB Catalog_5-29-08.txt");
?>
<?php
function find_dup($str, $arr)
{
	$key = array_search($str, $arr);
	echo $arr[$key] . "<br>";
}
?>
<html>
<head>
<title>compare</title>
</head>

<body>	
<table border = 1>
<th>current file</th>
	<?php
		foreach($currFile as $line)
		{?>	
			<tr>
			<td>
			<?php if(!find_dup($line, $prevFile));
			?>
			<td>
			</tr>
	  <?php }	  ?>
</table>
</body>
</html>

 

please advice

thanks

Link to comment
Share on other sites

i haven't tested it, but give it a try:

<?php
$txt1 = file("../file1.txt"); 
$txt2 = file("../file2.txt"); 

for($i=0; $i<count($txt1); $i++){
   if($txt1[$i] == $txt2[$i]){
   		// lines are the same
	echo $txt1[$i]." == ".$txt2[$i]; 
   } else {
   		// lines incorrect
                echo "lines not the same.";
                break;
   }
}  
?>

 

But be aware: Just one newline on the top and everything is invalid. ;)

Link to comment
Share on other sites

But be aware: Just one newline on the top and everything is invalid. ;)

 

Exactly, in order to avoid that:

$txt1 = file("../file1.txt", FILE_IGNORE_NEW_LINES); 
$txt2 = file("../file2.txt", FILE_IGNORE_NEW_LINES); 

::) ... ok, to more precisely: just add a new [_space_] line (with content, not an empty one) on the top and nothing will fit anymore ;)

Link to comment
Share on other sites

it depends on the usage. if you want to abort your script right after a wrong line is detected my version will have better performance. With array_diff you will also have to put file contents in an array first -> you will also have read the files first. You can also handle mistakes right away and continue depending on the results after every line. I don't think that it is better.

 

 

If you want to have all differences in an array at the end (what array_diff is made for) array_diff will be a petter choice. But you are right, i think homer.favenir wants to extract the difference ;)

 

 

Link to comment
Share on other sites

$txt1 = file("../file1.txt");

$txt2 = file("../file2.txt");

 

above code is anyways reading the complete files and putting in array. sorry if i got it wrong, but i think if you need to compare 2 files, you need to read them completely anyways.

Link to comment
Share on other sites

$txt1 = file("../file1.txt");

$txt2 = file("../file2.txt");

 

above code is anyways reading the complete files and putting in array. sorry if i got it wrong, but i think if you need to compare 2 files, you need to read them completely anyways.

 

Yes you are right! it puts contents in an array... but instead of file() you could also use fopen fread etc. and read it line by line :)

 

...But you are right, i think homer.favenir wants to extract the difference ;)

Link to comment
Share on other sites

$txt1 = file("../file1.txt");

$txt2 = file("../file2.txt");

 

above code is anyways reading the complete files and putting in array. sorry if i got it wrong, but i think if you need to compare 2 files, you need to read them completely anyways.

 

for comparing 2 files completely, we would need to read them completely anyways, besides fread will need them to have the data in same sequence, array will check even if data is scattered and not in sequence..but as you said it depends on the use..

 

:)

Link to comment
Share on other sites

thanks guys!

i need to extract only lines that are not in the other txtfile, thats why i need to compare them,

my txt file has 62200 lines. if line1 will search for 62200 lines and line2 will search again it will takes time or it will hang.

Link to comment
Share on other sites

it works, i compared it, extract the different line, but i have one more question.

it also extracts  the last line even if it is not different from the source.

and also i need it to be save in txt file as well....

 

<?php
$currFile = file("Portfolio extracted/sent/UFPB Catalog_5-28-08.txt");
$prevFile = file("Portfolio extracted/sent/UFPB Catalog_5-27-08.txt");
?>



<html>
<body>
<table border=1>
<tr>
<td><?php 
	print_r(array_diff($currFile, $prevFile));
  ?>
  
</td>
</tr>
</table>
</body>
</html>

please advice...

 

thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.