Search and Replace Text in Files

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
RalphM
Posts: 1932
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Re: Search and Replace Text in Files

Post 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).
Attachments
Test_Input.zip
(409 Bytes) Downloaded 39 times
Ralph :)
(OS: W11 22H2 Home x64 - XY: Current beta - Office 2019 32-bit - Display: 1920x1080 @ 125%)

kiwichick
Posts: 557
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Search and Replace Text in Files

Post 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.
Windows 10 Pro 22H2

Post Reply