This is really a very special search. A regular expression search is usually used to find a string on line X with another string on line X+1. It would be no problem to use a regular expression search to find a string starting at column Y on line X with another string starting also at column Y on line X+1. But the problem is that the starting column is not fixed if I understood the requirements for this special search. The rectangular block should be found anywhere in file. And last but not least two lines in file could contain the rectangular block to find more than once, like
- Code: Select all
CLD FW FW FW FW FW FW FW FW FW FW CL CL CL CL CL CL CL CL FW CL CL FW FW FW SC
CIG 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
I'm not sure but I think even a Perl regular expression is not capable for running such a find.
So I wrote an UltraEdit script for finding a string anywhere on a line with another string found exactly below the first found string.
- Code: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
var sFirstToFind = "";
var sSecondToFind = "";
var nActiveClipboard = UltraEdit.clipboardIdx;
// Get data from a previous run of this script stored in user clipboard 9
// if the active file is the same as on previous execution of the script.
var asFindData = UltraEdit.clipboardContent.split("\r\n");
if (asFindData == UltraEdit.activeDocument.path)
sFirstToFind = asFindData;
sSecondToFind = asFindData;
// Set caret to right by 1 character on first line if there is still
// a rectangular block selected from a previous script run to avoid
// finding the same block once again.
if ((UltraEdit.activeDocument.isSel()) && UltraEdit.columnMode && sFirstToFind.length)
// If there are no strings stored in user clipboard 9 from a previous
// execution of this script on active file ask user for the 2 strings.
sFirstToFind = UltraEdit.getString("Please enter string to find first:",1);
sSecondToFind = UltraEdit.getString("Please enter string to find second:",1);
// Define the parameters for the case-sensitive non regular
// expression find used to find first string in active file.
var bBlockFound = false;
// Search for the first string to find from current caret position to
// end of file in a loop until either the block is found or the first
// string can't be found anymore in active file.
// Get line and column number of caret which is at end of found string.
var nActiveLine = UltraEdit.activeDocument.currentLineNum;
var nActiveColumn = UltraEdit.activeDocument.currentColumnNum;
var nNextLine = nActiveLine+1;
var nNextColumn = nActiveColumn-sFirstToFind.length;
// Set caret to next line at same column as beginning of found string.
if ((UltraEdit.activeDocument.currentLineNum == nNextLine) &&
(UltraEdit.activeDocument.currentColumnNum == nNextColumn))
// Select everything to end of this line.
// Starts the selected string with second string to find?
if (UltraEdit.activeDocument.selection.indexOf(sSecondToFind) == 0)
// Rectangular block to find indeed found in active file. Select the
// rectangular block in column mode from bottom left to top right.
// Store the strings to find with file name with path in user
// clipboard 9 for next run of this script on same file.
UltraEdit.clipboardContent = UltraEdit.activeDocument.path + "\r\n" +
sFirstToFind + "\r\n" + sSecondToFind;
bBlockFound = true;
// Set caret back to end of first found string and search
// from this position once again for first string to find.
if (!bBlockFound) UltraEdit.activeDocument.top();
The script works only if the rectangular block to find does not contain horizontal tabs.
Copy the script code into a new ASCII file and save it for example with name FindTwoLinesRectangularBlock.js
in directory %APPDATA%\IDMComp\UltraEdit\scripts
which first must be created if not already existing. Next add this script to the Scripts List
without or with a hotkey or chord assigned to the script for quick execution by key.
On first run of this script on a file the user is prompted for the two strings of the two lines rectangular block to find anywhere in the file.
The script searches in file first for the first entered string and if found checks, if the line below at same starting column has the second entered string. If this is indeed the case, the two found strings are selected in column mode and as a special feature the name of the active file with path and the two entered strings are copied to user clipboard 9. Otherwise the script continues searching for next occurrence of first string in file until the two lines rectangular block is found or the first string could not be find up to end of file.
The script starts searching for next occurrence of first entered string always from current caret position in active file.
As a special feature the two entered strings to find must not be entered again if this script is executed on same file more than once and if on previous run the two lines rectangular block was indeed found in active file from current position of caret to end of file.
- The active file contains just the two lines as posted above and caret is at top.
- The script is the first time executed on active file. Therefore the user has to enter the two strings "FW CL CL" and " 8 8 8".
- The script selects the first found block from line 2 column 34 to line 1 column 42.
- The script is executed once again. The user does not need to enter the two strings again.
- The script selects the second found block from line 2 column 61 to line 1 column 69.
- The script is executed once more. Again the user does not need to enter the two strings.
- The script fails to find the two lines rectangular block once more. The caret is set back to top of file.
On next run of the script on this file the two strings of the block to find must be entered again as previous run was not successful to find the rectangular block.