Well, it would be possible to use the method as used also in
Script to find multiple items and output them with the small difference that the find string changes for every loop run and caret must be moved back to top of file after every find and copy append. But for 1000 id strings this method would be slow because of the 2000 display updates.
Also possible would be to jump from one id string in the input file to next and evaluate if the current id string is in the list from the CSV file. The block is copied ff this is the case. But if the input file has 20000 products this method would result in 20000 display updates making the script very slow.
But I have another idea. A very simple tagged regular expression replace is used to mark those products which are of interest. To reduce the number of replaces up to 50 product id strings are combined in an OR expression. When the entire list of id strings was applied to the file, the remaining products without a marker are removed. And finally the markers are removed from the remaining products.
Please note that the script below works only if
- first file - most left file on open file tabs bar - is the file containing the id strings separated by commas and no other characters like line terminators, or the list file is a simple text file which contains one id string per DOS terminated line.
- second file - right of most left file on open file tabs bar - is the file containing the product data.
The third file can be the script file executed with
Scripting - Run Active Script.
According to your request the script as posted below checks now if third character in first file is a hyphen character. If this is the case the first file is interpreted as file containing the identifcation strings and the second file is the file containing the product data. Otherwise the first file is interpreted as file with the product data and the second file contains the list of identification strings. It is not allowed that the identification strings contain characters which have a special meaning in Perl regular expression search strings as this would result in a not working script. Strings with just digits, letters, underscores and hyphens are no problem.
Update on 2012-10-22: The script supports now a second variant of input XML file as requested.
Code: Select all
// Are at least 2 files opened in UltraEdit?
if (UltraEdit.document.length > 1) {
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Get all id strings separated by a comma from first file if third
// character is a hyphen or from second file into an array of strings.
var asIDs;
var nDataFile = 0;
var nListFile = 1;
UltraEdit.document[0].gotoLine(1,3);
if (UltraEdit.document[0].isChar("-")) {
nDataFile = 1;
nListFile = 0;
}
UltraEdit.document[nListFile].selectAll();
if (UltraEdit.document[nListFile].selection.indexOf(",") > 0) {
asIDs = UltraEdit.document[nListFile].selection.split(",");
} else {
asIDs = UltraEdit.document[nListFile].selection.split("\r\n");
// Remove empty string at end of array if list file ended with a line termination.
if (asIDs[asIDs.length-1].length == 0) asIDs.pop();
}
// And copy entire contents of data file via clipboard 9 into a
// new file. The input files should not be modified for security.
UltraEdit.selectClipboard(9);
UltraEdit.document[nDataFile].selectAll();
UltraEdit.document[nDataFile].copy();
// Discard the selections in both files.
UltraEdit.document[nListFile].top();
UltraEdit.document[nDataFile].top();
// Paste the copied data into a new file.
UltraEdit.newFile();
UltraEdit.activeDocument.paste();
// Make sure the last line in new file ends with a line termination.
if (UltraEdit.activeDocument.isColNumGt(1)) {
UltraEdit.activeDocument.insertLine();
if (UltraEdit.activeDocument.isColNumGt(1)) {
UltraEdit.activeDocument.deleteToStartOfLine();
}
}
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
UltraEdit.activeDocument.top();
// Define the parameters for the case-sensitive Perl regular expression
// Replace All executions to mark the products of interest.
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.searchInColumn=false;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
// Run this loop until all id strings have been used in a Perl
// regular expression replace to mark the products of interest.
var nIdNum = 0;
var nMaxPerReplace = 50;
if (UltraEdit.activeDocument.findReplace.find("<kwdikos>")) {
var sSearchBegin = "^([ \\t]*<product)(>\\s+<kwdikos>)(";
var sSearchEnd = ")(<)";
UltraEdit.activeDocument.top();
} else {
var sSearchBegin = "^([ \\t]*<product)( id=\")(";
var sSearchEnd = ")(\")";
}
UltraEdit.activeDocument.findReplace.regExp=true;
while (nIdNum < asIDs.length)
{
var sSearchExp = sSearchBegin + asIDs[nIdNum];
var nToDoCount = asIDs.length - nIdNum;
if (nToDoCount > nMaxPerReplace) nToDoCount = nMaxPerReplace;
while (--nToDoCount) sSearchExp += '|' + asIDs[++nIdNum];
sSearchExp += sSearchEnd;
// The regular expression search string is now
// either ^([ \t]*<product)( id=")(id1|id2|id3|...|idn)(")
// or ^([ \t]*<product)(>\s+<kwdikos>)(id1|id2|id3|...|idn)(<)
// depending on existence of <kwdikos> anywhere in file.
UltraEdit.activeDocument.findReplace.replace(sSearchExp,"\\1#\\2\\3\\4");
nIdNum++;
}
// Remove now all products without # after tag string <product.
UltraEdit.activeDocument.findReplace.replace("(?s)^[ \\t]*<product[^#s].+?</product>\\s+","");
// Remove marker character # after <product in remaining content.
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.replace("<product#","<product");
}