Shoutbox

Searching text files - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Searching text files (/showthread.php?tid=90474)

Searching text files by Jimbo on 05-02-2009 at 03:45 PM

Whats the best way to search over 100 text files, for one word?


RE: Searching text files by blessedguy on 05-02-2009 at 03:50 PM

quote:
Originally posted by Jimbo
Whats the best way to search over 100 text files, for one word?
Can't windows search look for words inside those files?
RE: Searching text files by Jimbo on 05-02-2009 at 04:18 PM

I would like to search over 100 text files for a certain word, and then display that line, which has the certain word in, into a another text file?
Is that possible?


RE: Searching text files by Baggins on 05-02-2009 at 04:55 PM

quote:
Originally posted by Jimbo
I would like to search over 100 text files for a certain word, and then display that line, which has the certain word in, into a another text file?
Is that possible?
Linux + some combination of cat and grep
RE: Searching text files by stoshrocket on 05-02-2009 at 07:15 PM

It'd be quite easy in PHP...


RE: Searching text files by blessedguy on 05-02-2009 at 07:20 PM

quote:
Originally posted by nimicitor
Open each file and press Ctrl + F?
100 times?! =P

quote:
Originally posted by methos
It'd be quite easy in PHP...
How? =O
RE: Searching text files by Jimbo on 05-02-2009 at 07:30 PM

Yeah i know its possible in linux, just wondering if there was any way possible in windows?


RE: Searching text files by L. Coyote on 05-02-2009 at 08:05 PM

Take a look at this article, and also look at all the comments (lots of related programs, including cygwin :P).


RE: Searching text files by Jarrod on 05-02-2009 at 10:12 PM

python would be eaiser, use regex in python:), if you need me to write something, pm or post


RE: Searching text files by WDZ on 05-03-2009 at 01:01 AM

If all the text files are in the same directory, open a command prompt in that directory and run...

find "word" *.txt > ..\results.txt

(The results file is in the parent directory so it won't be included in the search =p)

You could also use a text editor with a "find in files" feature, like PSPad or Notepad++ or EditPlus.


RE: Searching text files by Jimbo on 05-03-2009 at 05:45 PM

Thanks WDZ :)


RE: Searching text files by stoshrocket on 05-03-2009 at 07:27 PM

quote:
Originally posted by blessedguy
quote:
Originally posted by methos
It'd be quite easy in PHP...
How? =O
Not nearly as easy as WDZ's method above, but just for completion and curiosity...
php code:
$needle = 'needle'; //value to search in the text files
$dir = './dir/'; //dir to folder containing text files

$files = scandir($dir);
$arr_count = count($files);
$i = 0;
$cur_file = current($files);

while ($i<$arr_count){
    $ext = substr($cur_file, strrpos($cur_file,'.') + 1);
    if($ext == 'txt'){
        $line_array = file($dir.$cur_file);
        $line_count = count(file($dir.$cur_file));
        $n = 1;
        $cur_line = current($line_array);
        while ($n <= $line_count){
            if(strpos($cur_line,$needle) !== FALSE){
                $line_quote = str_replace($needle,"<b>".$needle."</b>",$cur_line);
                echo"Found value in $dir"."$cur_file <br /> $n - $line_quote";
                exit;
            }
            $n++;
            $cur_line = next($line_array);
        }
    }
    $i++;
    $cur_file = next($files);
}

Creates an array of the contents of ./dir then progressively checks if each value is a text file, if so, each line of this text file is read into an array, which is then progressively searched for the search value $needle. When $needle is found, it stops searching and the path to the file is echo'd, along with the line number and quote of the line with $needle in bold. Can be easily modified to create a file results.txt with this info in, and to search through all files etc.

:happy:
RE: Searching text files by -dt- on 05-04-2009 at 12:15 AM

quote:
Originally posted by methos
quote:
Originally posted by blessedguy
quote:
Originally posted by methos
It'd be quite easy in PHP...
How? =O
Not nearly as easy as WDZ's method above, but just for completion and curiosity...
php code:
$needle = 'needle'; //value to search in the text files
$dir = './dir/'; //dir to folder containing text files

$files = scandir($dir);
$arr_count = count($files);
$i = 0;
$cur_file = current($files);

while ($i<$arr_count){
    $ext = substr($cur_file, strrpos($cur_file,'.') + 1);
    if($ext == 'txt'){
        $line_array = file($dir.$cur_file);
        $line_count = count(file($dir.$cur_file));
        $n = 1;
        $cur_line = current($line_array);
        while ($n <= $line_count){
            if(strpos($cur_line,$needle) !== FALSE){
                $line_quote = str_replace($needle,"<b>".$needle."</b>",$cur_line);
                echo"Found value in $dir"."$cur_file <br /> $n - $line_quote";
                exit;
            }
            $n++;
            $cur_line = next($line_array);
        }
    }
    $i++;
    $cur_file = next($files);
}

Creates an array of the contents of ./dir then progressively checks if each value is a text file, if so, each line of this text file is read into an array, which is then progressively searched for the search value $needle. When $needle is found, it stops searching and the path to the file is echo'd, along with the line number and quote of the line with $needle in bold. Can be easily modified to create a file results.txt with this info in, and to search through all files etc.

:happy:
its even easier than that...

php code:
<?php

$dir = "D:/";
$toFind = "xxx";

foreach(new DirectoryIterator($dir) as $file){
    if(!$file->isDir()){
        $ext = trim(strrchr($file, '.'), ".");
        if($ext == "txt"){   
            $fp = fopen($file->getPathname(), 'r');
            while($fp && !feof($fp)){
                $line = stream_get_line($fp, 2048, "\n");
                if(strpos($line, $toFind) !== false){
                    echo $file . "\n";
                }
            }
            fclose($fp);
        }
    }
}




?>

RE: Searching text files by stoshrocket on 05-04-2009 at 12:35 AM

quote:
Originally posted by -dt-

its even easier than that...

php code:
<?php

$dir = "D:/";
$toFind = "xxx";

foreach(new DirectoryIterator($dir) as $file){
    if(!$file->isDir()){
        $ext = trim(strrchr($file, '.'), ".");
        if($ext == "txt"){   
            $fp = fopen($file->getPathname(), 'r');
            while($fp && !feof($fp)){
                $line = stream_get_line($fp, 2048, "\n");
                if(strpos($line, $toFind) !== false){
                    echo $file . "\n";
                }
            }
            fclose($fp);
        }
    }
}
?>

Very elegant, I never thought of using foreach to work through each file or even stream_to_get_line! The more you use the more you learn!