Author Topic: Find & Replace or MultiScript to edit & output XML?  (Read 15318 times)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Find & Replace or MultiScript to edit & output XML?
« on: October 31, 2022, 14:12:49 »
The question is, is the following task beyond the scope of F&R (&/or MS)? 
Basically:-
I have .xml's that need search & replace operations.
I need to check multiple files for each of the entries & replace if/where found.
All SAR terms are not in all files... Both searches & replacements will change over time.  ::)

The source for the search & the replacements is currently a CSV, >1000 rows/entries.
The job is thus:-
Code: [Select]
Search for x where x is entry in CSV column 1;
   If CSV column 1 entry found, replace with CSV column 2 entry;  (Rows being SAR pairs 1|1, 2|2, 3|3 etc)
OR
   If CSV column 1 entry not found;
Step down to next row, loop til end of CSV
Resulting XML to be saved with a new name
Next file

So far in F&R I tried adding all column 1 entries to find & all column 2 entries to replace with "Find many, Replace with many" selected.
Result is error 87 see screen.
I'm guessing this is because there are mismatches & F&R doesn't step down to next term rather than there being too many entries.....?

...........Maybe I can leverage F&R with MS or something?

Any simple way to do this task?  Thanks in advance :)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #1 on: November 04, 2022, 13:49:12 »
87 is a generic error that means invalid parameter

It hard to say what goes wrong, depends on that input and what options used and such.
Don't really understand what you are trying to do.. you say xml files but and then you say CSV file
But are text file.. the search and replace things work on text files. but do not handle xml/cvs files in any special way, but as pure text file.

if you want to do pairs of find and replace.. there must be correct number of them..
and option for find and replace is the second one "Find many, replace with many"


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #2 on: November 05, 2022, 14:57:20 »
Thanks for the reply Mathias.

CSV is original source of search & replace terms; XML is target.  (I just copied CSV columns of data out to the F&R dialog, & gave it xml as target.)
I've listed the 'original operation' with CSV & XML labels as I wondered if MultiScript could handle those formats instead of using F&R for the job.

Basically I can exclude the CSV by transferring all the data to F&R input & output fields.
F&R seeing XML as TXT is not a problem as no formatting is altered. 
It seems to work if all search terms are found. 
So I guess the error must be because there is no 100% match between all CSV terms & all XML targets.

Problem is all XMLs are different i.e. not all terms are in all XMLs.  Because of this I need an EITHER OR function,
EITHER search / find/ replace
OR search / not find
THEN search next term in list
REPEAT til end of list.

I want to be able to step over non-matches or matches that have already been made....  I know this is possible but maybe not via MC.

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #3 on: December 06, 2022, 15:35:58 »
So......  I'm guessing no non-overlapping SaR using script or MS rename tools is possible then...  :(

AlanJB

  • VIP Member
  • *****
  • Posts: 436
  • VERY old Programmer
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #4 on: December 06, 2022, 18:40:17 »
Hi Ulf.

Do you have a decent text editor that supports macros (Notepad++, UltraEdit, etc)?

As a programmer, I believe it would be quicker and less effort to use one of those as the tool for the job.

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4423
    • View Profile
    • Multi Commander
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #5 on: December 07, 2022, 11:07:09 »
So......  I'm guessing no non-overlapping SaR using script or MS rename tools is possible then...  :(

I don't know.

I don't understand what you are doing. So I can't setup a test to test it.



Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #6 on: December 07, 2022, 14:16:28 »
Thanks for the replies.

@AlanJB
I have looked at Notepad++ but I think dnGREP may be the way to go IF I can figure out the scripting of loops for the system the dev has just implemented....
His code example is
Code: [Select]
set folder C:\Repos\testFiles\test\scriptTest\
set pathtomatch *.cs
set searchfor string
set replacewith String
search
replace
set searchfor bool
set replacewith Boolean
search
replace
  which of course doesn't have an ELSE type loop implemented.  Iteration being what I'm looking for.
 
@Mathias
I'm trying to do a non-overlapping search.

My searches are for different terms which may or may not be in the target files.

eg I have 1000 search terms & each file contains 300 possible matches.
I must SAR for each term but it may not find a match because each file is different.
Thus I need to search all terms for every file & replace as found. 

The search process must continue to run through the list if a search term is NOT matched until it hits the end of the search list. 
The current system you have in Find & Replace only works where every search term is present (overlapping search), a non-match halts the process execution.
Great dialog etc but not suitable for this taskwithout a switch to loop past non-matches... so I wondered about MScript as then I might be able to define loops.

ELSE & IF seem to be script terms for this type of loop but don't know if I can apply them here...

If I must write a script to loop 1500+ terms then I'd like some familiarity/hope of getting it right.
« Last Edit: December 07, 2022, 16:22:08 by Ulfhednar »

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 542
  • Old Skull
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #7 on: December 08, 2022, 16:44:24 »
Well, S&R works:

Code: [Select]
@var $sel_files = GetSourceSelectedPaths();
@var $sel_count = arrayCount($sel_files);
@var $find_text = "aaa\\nbbb";
@var $repl_text = "xxx\\nyyy";

@var $n;
for($n = 0; $n < $sel_count; $n++)
{
  @var $file = $sel_files[$n];
#  @var $new_file = PathGetPathPart($file) ^ PathGetNamePart($file, 1) + "_new" + PathGetFileExtPart($file);
  @var $new_file = "*_new.*";

  MC.Utils.FindAndReplace MODE="Many" FIND={$find_text} REPLACEWITH={$repl_text} FILE="{$file}" TARGET="{$new_file}" SILENT REPLACEALL OVERWRITE
}

The full automation requires parsing CSV-file, of course. If its structure is known and "stable", it shouldn't be hard.

UPD. Actually, parsing CSV might be quite hard (in MC).
« Last Edit: December 08, 2022, 17:14:11 by Jungle »

Jungle

  • Contributor
  • VIP Member
  • *****
  • Posts: 542
  • Old Skull
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #8 on: December 08, 2022, 20:37:18 »
Here's updated version with simple CSV parser. Please, don't use in production! :)

Usage example:
 - paste this script into the MultiScript debugger
 - focus your CSV-file on the target panel
 - switch to source panel and select xml-files
 - return to debugger and run

Code: [Select]
# parse csv S&R file
function ParseCSV($file, $separators, $quote_chars)
{
  if ( !(StrLen($separators) == 1) )
  {
    $separators = ",";
  }

  if ( StrLen($quote_chars) == 0 )
  {
    $quote_chars = "\"";
  }

  @var $reg_ex = "^([" + $quote_chars + "])(.*)\\1$";

  @var $lines = LoadArray($file);
  @var $lines_cnt = arrayCount($lines);

  # single-string multiline S&R patterns
  # $pattern[0] - search pattern, $pattern[1] - replace pattern
  @var $pattern[2] = {"", ""};

  @var $i;
  for ($i = 0; $i < $lines_cnt; $i++)
  {
    # $pair[0] - search pattern, $pair[1] - replace pattern
    @var $pair = StrSplit($lines[$i], $separators);

    # invalid line
    if ( !(arrayCount($pair) == 2) )
    {
      continue;
    }

    # append unquoted patterns
    $pattern[0] = $pattern[0] + StrRegExpReplace( $pair[0], $reg_ex, "$2" ) + "\n";
    $pattern[1] = $pattern[1] + StrRegExpReplace( $pair[1], $reg_ex, "$2" ) + "\n";
  }

  return $pattern;
}

###

@var $sar_file    = GetTargetFocusPath();
@var $sar_pattern = ParseCSV($sar_file, ";", "\"`");

if ( ( StrLen($sar_pattern[0]) * StrLen($sar_pattern[1]) ) > 0 )
{
  @var $sel_files = GetSourceSelectedPaths();
  @var $sel_count = arrayCount($sel_files);

  @var $n;
  for($n = 0; $n < $sel_count; $n++)
  {
    @var $file = $sel_files[$n];
    @var $new_file = "*_new.*";

    MC.Utils.FindAndReplace MODE="Many" FIND={$sar_pattern[0]} REPLACEWITH={$sar_pattern[1]} FILE="{$file}" TARGET="{$new_file}" SILENT REPLACEALL OVERWRITE
  }
}

I haven't stress-tested it.
« Last Edit: December 09, 2022, 14:11:22 by Jungle »

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #9 on: December 09, 2022, 15:42:56 »
Thanks for your extensive input Jungle  ;)

As you are a MScript maestro that has definitely given me something to work with!   8)
Once I have tried this out I can start filling in any gaps / quirks I encounter.

BTW I was using the CSV as source for the matches & then editing XML with the SAR output. 
This worked OK as the replacements are all contained by the existing XML structure & didn't impact the XML layout. 

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Find & Replace or MultiScript to edit & output XML?
« Reply #10 on: December 09, 2022, 16:12:39 »
Well, S&R works:

Code: [Select]
@var $sel_files = GetSourceSelectedPaths();
@var $sel_count = arrayCount($sel_files);
@var $find_text = "aaa\\nbbb";
@var $repl_text = "xxx\\nyyy";

@var $n;
for($n = 0; $n < $sel_count; $n++)
{
  @var $file = $sel_files[$n];
#  @var $new_file = PathGetPathPart($file) ^ PathGetNamePart($file, 1) + "_new" + PathGetFileExtPart($file);
  @var $new_file = "*_new.*";

  MC.Utils.FindAndReplace MODE="Many" FIND={$find_text} REPLACEWITH={$repl_text} FILE="{$file}" TARGET="{$new_file}" SILENT REPLACEALL OVERWRITE
}

The full automation requires parsing CSV-file, of course. If its structure is known and "stable", it shouldn't be hard.

UPD. Actually, parsing CSV might be quite hard (in MC).

Leveraging  MC.Utils.FindAndReplace tool halts with error when trying non-overlapping search.  I presume this is the same error as most GREP tools I have tried  ie a non-match ends the forward stepping.
The only tool I found which does this out-of-the-box AFAIK is PowerGREP (https://www.powergrep.com/), this dev does some excellent work.
Unfortunately he's waaaaay out of my price-range esp. for a small personal project.

....But if I can find a tool I can understand like MultiScript, it is something I'd like to learn to do for myself, so all input is appreciated ;)