Page 2 of 2

Re: Search and Replace Text in Files

Posted: 29 Jul 2022 12:34
by RalphM
Regexreplace is your friend and darn fast too.

Below is an excerpt from one of my text file conversion scripts that I use to convert messy log files into tabbed content which can be imported into Excel columns later.

I had started out with loops over the whole input file for each replacement and it took minutes to run on input files with a few 1000 lines.
After rewriting it to use regexreplace instead, it runs for mere seconds now on the same files.

It takes a while to get the regexes working if you're not an expert but it's time well invested as they are incredibly powerful and can do miracles.

Code: Select all

foreach ($item, <selitems |>) {
        $sInput = readfile ($item);
        $sInput = regexreplace($sInput, "^No\.\tDevice No\..*$\r?\n", ""); //Remove header line
        $sInput = regexreplace($sInput, "^\d+\t.*\t\t(.*)\t(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})\t\d\t(.*)\t(.*)\t(.*) .*$", "$2<tab>$3<tab>Rego: $1 - Dir: $4 - Val: $5 - List: $6"); //Reorder all fields 
        $sInput = regexreplace($sInput, "\r?\n$"); //Remove empty last line
        writefile (gpc($item, "path") . "\" . gpc($item, "base") . "_conv.txt", $sInput); 
 }
I attach a little test file that should work with the above code if you're interested (extract it somewhere, select it and run the above code, then compare the original file with the newly created one).

Re: Search and Replace Text in Files

Posted: 08 Sep 2022 07:16
by kiwichick
RalphM wrote: 29 Jul 2022 12:34 Regexreplace is your friend and darn fast too.

Below is an excerpt from one of my text file conversion scripts that I use to convert messy log files into tabbed content which can be imported into Excel columns later.

I had started out with loops over the whole input file for each replacement and it took minutes to run on input files with a few 1000 lines.
After rewriting it to use regexreplace instead, it runs for mere seconds now on the same files.

It takes a while to get the regexes working if you're not an expert but it's time well invested as they are incredibly powerful and can do miracles.

Code: Select all

foreach ($item, <selitems |>) {
        $sInput = readfile ($item);
        $sInput = regexreplace($sInput, "^No\.\tDevice No\..*$\r?\n", ""); //Remove header line
        $sInput = regexreplace($sInput, "^\d+\t.*\t\t(.*)\t(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})\t\d\t(.*)\t(.*)\t(.*) .*$", "$2<tab>$3<tab>Rego: $1 - Dir: $4 - Val: $5 - List: $6"); //Reorder all fields 
        $sInput = regexreplace($sInput, "\r?\n$"); //Remove empty last line
        writefile (gpc($item, "path") . "\" . gpc($item, "base") . "_conv.txt", $sInput); 
 }
I attach a little test file that should work with the above code if you're interested (extract it somewhere, select it and run the above code, then compare the original file with the newly created one).
I am so sorry you didn't get a reply from me. I don't know how I missed your suggestion but I did. I've only just found it now. I shall check out your code, thank you very much.