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);
}