I suppose the characters are separated by a horizontal tab character and not by a space character.
Are on every line really only characters?
The script could be much easier and faster if on the lines are really only single characters separated by tabs. But I suppose that there are words on every line and therefore wrote the script for word comparisons and not for character searches. That makes the script slower, but makes it work for characters and words.
First file open (most left on open file tabs bar) must be the file to search in for the words or characters.
Second file open must be the file with the words to find.
The third file open can be the script file executed with command
Scripting - Run Active Script.
- Code: Select all
if (UltraEdit.document.length > 1)
{
// Define the environment for the script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.document[0].hexOff(); // File to search in for words.
UltraEdit.document[1].hexOff(); // File with the words to find.
// Select all and load the file contents into an array of lines.
UltraEdit.document[0].selectAll();
if (UltraEdit.document[0].isSel())
{
var asLines = UltraEdit.document[0].selection.split("\r\n");
UltraEdit.document[0].top(); // Discards the selection.
UltraEdit.document[1].selectAll();
if (UltraEdit.document[1].isSel())
{
var asWords = UltraEdit.document[1].selection.split("\r\n");
UltraEdit.document[1].top(); // Discards the selection.
// Remove last string if it is an empty string because
// of second file ends with a line termination.
if (asWords[asWords.length-1] == "") asWords.pop();
// Create a new empty array for the results.
var asResults = new Array(asWords.length);
// Search in every line for the defined words. The words
// are separated by a single tab character on every line.
for (var nLineNum = 0; nLineNum < asLines.length; nLineNum++)
{
if (!asLines[nLineNum].length) continue; // Ignore empty lines.
// Convert line number to decimal string.
var sLineNum = (nLineNum+1).toString();
// Split the line up into an array of word strings.
var asStrings = asLines[nLineNum].split("\t");
// Run a case sensitive word comparison from first to last
// word in the words array on the strings of current line.
for (var nWord = 0; nWord < asWords.length; nWord++)
{
var sWord = asWords[nWord];
// Search in strings array for the current word.
for (var nString = 0; nString < asStrings.length; nString++)
{
if (asStrings[nString] == sWord)
{
// Was this word found already once on any line?
if (asResults[nWord] != null)
{
// Yes, append the line number information.
asResults[nWord] += "\t" + sLineNum;
}
else // This word is found the first time. Create
{ // the results string with word and line number.
asResults[nWord] = sWord + "\t" + sLineNum;
}
// Remove this word from the strings array for reducing
// the number of compares on the following words.
asStrings.splice(nString,1);
break; // Exit comparison loop.
}
} // Continue with next word on current line.
} // Continue with next line.
}
// Write the results into a new file.
UltraEdit.newFile();
UltraEdit.activeDocument.unixMacToDos();
var sResult = asResults.join("\r\n") + "\r\n";
UltraEdit.activeDocument.write(sResult);
UltraEdit.activeDocument.top();
}
}
}