Many of the macros posted by me contain the sequence:
Code: Select all
Bottom
IfColNumGt 1
InsertLine
IfColNumGt 1
DeleteToStartofLine
EndIf
EndIf
Top
This code sequence can be found also in my macro reference file which can be downloaded from sticky macros forum topic
Macro examples and reference for beginners and experts.
This macro code makes sure that the file does not end with a string, but with a line which means the file ends with a line termination. In your case it would be necessary to run this code sequence once on
a.txt and on
cities.txt before starting the replaces.
don_bradman wrote:I'm trying a create a script which on calling will open the "cities.txt" and remove any matching data from the current file using the "cities.txt" as a reference.
This explanation means on using the examples search first for
<city>Seattle</city> from
cities.txt in
a.txt and remove it when really found. Next search for
<city>Orillia</city> from from
cities.txt in
a.txt and remove all found occurrences. And so on until all lines from
cities.txt are used on
a.txt. Macros and scripts for that task are posted at lot as I wrote already once.
The result would be for
a.txt:
Code: Select all
<city>Alabama</city>
<city>Quebec</city>
<city>Alberta</city>
<city>Victoria</city>
<city>Queensland</city>
<city>Assam</city>
<city>Iowa</city>
<city>New South Wales</city>
This output can be get with the following script:
Code: Select all
// This script as coded requires at least UltraEdit for Windows v16.00
// or UEStudio v10.00. Former versions are not supported without code
// modification because of activeDocumentIdx and currentColumnNum usage.
// Name of the cities list file with full path defined as global constant.
var g_sCitiesFileName = "D:\\cities.txt";
// Global variables for document index of data file to modify as well
// as an array with line and column number inside cities list file in
// case of being already opened on script start.
var g_nDataFile;
var g_anListCaretPos = [0,0];
// Function for finding cities list file in opened files or for opening it.
// The cities list file is the active file on successfully finding/opening it.
function OpenListFile ()
{
// The active file is by default the data file to modify.
g_nDataFile = UltraEdit.activeDocumentIdx;
// First find out if the list file is opened already in UltraEdit.
var sLowerCasesCitiesFileName = g_sCitiesFileName.toLowerCase();
for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
{
// Is the list file opened already in UltraEdit?
if (UltraEdit.document[nDocIndex].path.toLowerCase() == sLowerCasesCitiesFileName)
{
var nListFile = nDocIndex;
g_anListCaretPos[0] = UltraEdit.document[nDocIndex].currentLineNum;
g_anListCaretPos[1] = UltraEdit.document[nDocIndex].currentColumnNum;
// Is the active file the list file instead of the data file?
if (nListFile == g_nDataFile)
{
if (UltraEdit.document.length != 2)
{
UltraEdit.messageBox("Active file is the cities list file and there are more than 2 files opened.\n\nPlease make a different file active before running the script again.");
return false;
}
g_nDataFile = (nListFile == 0) ? 1 : 0;
}
else // Make the list file the active file to avoid display
{ // refreshes on executing later the replaces when the
// document windows are displayed maximized.
UltraEdit.document[nListFile].setActive();
}
return true;
}
}
// The list file must be opened now and will be appended to document array.
// So its document index will be the current number of opened files and
// the number of opened files increases on successfully openening the file.
nListFile = UltraEdit.document.length;
UltraEdit.open(g_sCitiesFileName);
// Could the list file with the cities be opened successfully?
if (UltraEdit.document.length > nListFile)
{
return true;
}
UltraEdit.messageBox("Failed to open file: " + g_sCitiesFileName);
return false;
}
// Main function for reading the list file and running the replaces.
if (UltraEdit.document.length > 0) // Is any file opened?
{
if (OpenListFile())
{
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
// Load all lines in active cities list file into an array of strings.
UltraEdit.activeDocument.trimTrailingSpaces();
UltraEdit.activeDocument.selectAll();
if (UltraEdit.activeDocument.isSel())
{
var sLineTerm;
if (UltraEdit.activeDocument.lineTerminator <= 0) sLineTerm = "\r\n";
else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
else sLineTerm = "\r";
var asCities = UltraEdit.activeDocument.selection.split(sLineTerm);
// Make sure the last line of the data file has also a line termination.
UltraEdit.document[g_nDataFile].bottom();
if (UltraEdit.document[g_nDataFile].isColNumGt(1))
{
UltraEdit.document[g_nDataFile].insertLine();
if (UltraEdit.document[g_nDataFile].isColNumGt(1))
{
UltraEdit.document[g_nDataFile].deleteToStartOfLine();
}
}
// Determine the line termination type of active file.
if (UltraEdit.document[g_nDataFile].lineTerminator <= 0) sLineTerm = "\r\n";
else if (UltraEdit.document[g_nDataFile].lineTerminator == 1) sLineTerm = "\n";
else sLineTerm = "\r";
// Move caret to top of data file to modify and define the
// parameters for the replaces executed in the FOR loop below.
UltraEdit.document[g_nDataFile].top();
UltraEdit.ueReOn();
UltraEdit.document[g_nDataFile].findReplace.mode=0;
UltraEdit.document[g_nDataFile].findReplace.matchCase=false;
UltraEdit.document[g_nDataFile].findReplace.matchWord=false;
UltraEdit.document[g_nDataFile].findReplace.regExp=true;
UltraEdit.document[g_nDataFile].findReplace.searchDown=true;
UltraEdit.document[g_nDataFile].findReplace.searchInColumn=false;
UltraEdit.document[g_nDataFile].findReplace.preserveCase=false;
UltraEdit.document[g_nDataFile].findReplace.replaceAll=true;
UltraEdit.document[g_nDataFile].findReplace.replaceInAllOpen=false;
var bInvalidLines = false;
for (var nCity = 0; nCity < asCities.length; nCity++)
{
// Ignore empty lines in cities list file.
if (!asCities[nCity].length) continue;
// Process only lines from list file matching the following
// pattern and log all other invalid lines in output window.
if (asCities[nCity].search(/^[\t ]*<city>.+?<\/city>$/) >= 0)
{
var sSearchExp = "%[^t ]++";
sSearchExp += asCities[nCity].replace(/^[\t ]*(<city>.+?<\/city>)$/,"$1");
sSearchExp += "[^t ]++" + sLineTerm;
UltraEdit.document[g_nDataFile].findReplace.replace(sSearchExp,"");
}
else
{
if (!bInvalidLines)
{
UltraEdit.outputWindow.clear();
UltraEdit.outputWindow.write("1 or more invalid lines found in cities list file:\n");
bInvalidLines = true;
}
UltraEdit.outputWindow.write(g_sCitiesFileName + "(" + (nCity+1).toString(10) + "): " + asCities[nCity]);
}
}
// Was the cities list file opened by the script?
if (g_anListCaretPos[0] == 0)
{
// Make the data file the active file and close list file.
var nListFile = UltraEdit.activeDocumentIdx;
UltraEdit.document[g_nDataFile].setActive();
UltraEdit.closeFile(UltraEdit.document[nListFile].path,2);
}
else
{
// Restore initial position in list file
// and make the data file the active file.
UltraEdit.activeDocument.gotoLine(g_anListCaretPos[0],g_anListCaretPos[1]);
UltraEdit.document[g_nDataFile].setActive();
}
// Was at least 1 invalid line written to output window?
if (bInvalidLines)
{
// Make the output window visible and disable
// printing status information into output window.
if (UltraEdit.outputWindow.visible == false)
{
UltraEdit.outputWindow.showWindow(true);
}
UltraEdit.outputWindow.showStatus=false;
}
}
}
}
After having finished the script above I compared it with the output example and could see that the expected output is the opposite. Instead of removing from
a.txt all cities listed in
cities.txt, the expected output should be that
a.txt contains finally only cities listed in
cities.txt.
So I modified the script to:
Code: Select all
// This script as coded requires at least UltraEdit for Windows v16.00
// or UEStudio v10.00. Former versions are not supported without code
// modification because of activeDocumentIdx and currentColumnNum usage.
// Name of the cities list file with full path defined as global constant.
var g_sCitiesFileName = "D:\\cities.txt";
// Global variables for document index of data file to modify as well
// as an array with line and column number inside cities list file in
// case of being already opened on script start.
var g_nDataFile;
var g_anListCaretPos = [0,0];
// Function for finding cities list file in opened files or for opening it.
// The cities list file is the active file on successfully finding/opening it.
function OpenListFile ()
{
// The active file is by default the data file to modify.
g_nDataFile = UltraEdit.activeDocumentIdx;
// First find out if the list file is opened already in UltraEdit.
var sLowerCasesCitiesFileName = g_sCitiesFileName.toLowerCase();
for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
{
// Is the list file opened already in UltraEdit?
if (UltraEdit.document[nDocIndex].path.toLowerCase() == sLowerCasesCitiesFileName)
{
var nListFile = nDocIndex;
g_anListCaretPos[0] = UltraEdit.document[nDocIndex].currentLineNum;
g_anListCaretPos[1] = UltraEdit.document[nDocIndex].currentColumnNum;
// Is the active file the list file instead of the data file?
if (nListFile == g_nDataFile)
{
if (UltraEdit.document.length != 2)
{
UltraEdit.messageBox("Active file is the cities list file and there are more than 2 files opened.\n\nPlease make a different file active before running the script again.");
return false;
}
g_nDataFile = (nListFile == 0) ? 1 : 0;
}
else // Make the list file the active file to avoid display
{ // refreshes on executing later the replaces when the
// document windows are displayed maximized.
UltraEdit.document[nListFile].setActive();
}
return true;
}
}
// The list file must be opened now and will be appended to document array.
// So its document index will be the current number of opened files and
// the number of opened files increases on successfully openening the file.
nListFile = UltraEdit.document.length;
UltraEdit.open(g_sCitiesFileName);
// Could the list file with the cities be opened successfully?
if (UltraEdit.document.length > nListFile)
{
return true;
}
UltraEdit.messageBox("Failed to open file: " + g_sCitiesFileName);
return false;
}
// Main function for reading the list file and running the replaces.
if (UltraEdit.document.length > 0) // Is any file opened?
{
if (OpenListFile())
{
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
// Load all lines in active cities list file into an array of strings.
UltraEdit.activeDocument.trimTrailingSpaces();
UltraEdit.activeDocument.selectAll();
if (UltraEdit.activeDocument.isSel())
{
var sLineTerm;
if (UltraEdit.activeDocument.lineTerminator <= 0) sLineTerm = "\r\n";
else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
else sLineTerm = "\r";
var asCities = UltraEdit.activeDocument.selection.split(sLineTerm);
// Make sure the last line of the data file has also a line termination.
UltraEdit.document[g_nDataFile].bottom();
if (UltraEdit.document[g_nDataFile].isColNumGt(1))
{
UltraEdit.document[g_nDataFile].insertLine();
if (UltraEdit.document[g_nDataFile].isColNumGt(1))
{
UltraEdit.document[g_nDataFile].deleteToStartOfLine();
}
}
// Determine the line termination type of active file.
if (UltraEdit.document[g_nDataFile].lineTerminator <= 0) sLineTerm = "\r\n";
else if (UltraEdit.document[g_nDataFile].lineTerminator == 1) sLineTerm = "\n";
else sLineTerm = "\r";
// Move caret to top of data file to modify and define the
// parameters for the replaces executed in the FOR loop below.
UltraEdit.document[g_nDataFile].top();
UltraEdit.ueReOn();
UltraEdit.document[g_nDataFile].findReplace.mode=0;
UltraEdit.document[g_nDataFile].findReplace.matchCase=false;
UltraEdit.document[g_nDataFile].findReplace.matchWord=false;
UltraEdit.document[g_nDataFile].findReplace.regExp=true;
UltraEdit.document[g_nDataFile].findReplace.searchDown=true;
UltraEdit.document[g_nDataFile].findReplace.searchInColumn=false;
UltraEdit.document[g_nDataFile].findReplace.preserveCase=false;
UltraEdit.document[g_nDataFile].findReplace.replaceAll=true;
UltraEdit.document[g_nDataFile].findReplace.replaceInAllOpen=false;
var bInvalidLines = false;
for (var nCity = 0; nCity < asCities.length; nCity++)
{
// Ignore empty lines in cities list file.
if (!asCities[nCity].length) continue;
// Process only lines from list file matching the following
// pattern and log all other invalid lines in output window.
if (asCities[nCity].search(/^[\t ]*<city>.+?<\/city>$/) >= 0)
{
var sSearchExp = "%^([^t ]++";
sSearchExp += asCities[nCity].replace(/^[\t ]*(<city>.+?<\/city>)$/,"$1");
sSearchExp += "^)";
UltraEdit.document[g_nDataFile].findReplace.replace(sSearchExp,"#^1");
}
else
{
if (!bInvalidLines)
{
UltraEdit.outputWindow.clear();
UltraEdit.outputWindow.write("1 or more invalid lines found in cities list file:\n");
bInvalidLines = true;
}
UltraEdit.outputWindow.write(g_sCitiesFileName + "(" + (nCity+1).toString(10) + "): " + asCities[nCity]);
}
}
// Remove all lines not marked with # at beginning of the line.
UltraEdit.document[g_nDataFile].findReplace.replace("%[~#^r^n]*"+sLineTerm,"");
// Remove the marker character # from beginning of the remaining lines.
UltraEdit.document[g_nDataFile].findReplace.replace("%#","");
// Was the cities list file opened by the script?
if (g_anListCaretPos[0] == 0)
{
// Make the data file the active file and close list file.
var nListFile = UltraEdit.activeDocumentIdx;
UltraEdit.document[g_nDataFile].setActive();
UltraEdit.closeFile(UltraEdit.document[nListFile].path,2);
}
else
{
// Restore initial position in list file
// and make the data file the active file.
UltraEdit.activeDocument.gotoLine(g_anListCaretPos[0],g_anListCaretPos[1]);
UltraEdit.document[g_nDataFile].setActive();
}
// Was at least 1 invalid line written to output window?
if (bInvalidLines)
{
// Make the output window visible and disable
// printing status information into output window.
if (UltraEdit.outputWindow.visible == false)
{
UltraEdit.outputWindow.showWindow(true);
}
UltraEdit.outputWindow.showStatus=false;
}
}
}
}
This script modifies
a.txt to:
Code: Select all
<city>Orillia</city>
<city>Goa</city>
<city>Paris</city>
That result is also different to posted expected output because of last line
<city>Iowa</city>, but this is most likely a mistake made on creating the output example block.
As I know that people from India makes everything wrong which can be made wrong (learned over years here in forum and also at work with our colleagues from India offices), I added lots of extra code which would be in general not necessary to make the scripts as error proof as possible.
It took me 3 hours to write both scripts, test them and write this reply. I hope I don't need to do anything else because you find out now with having the scripts that you want something different which would be also typical for people from India.