Find RegExp "%^(*^)^p*$"
Replace All "^1"
is an UltraEdit regular expression replace all command. The general special characters like
^p are explained in help for example on the page
Find command (Search menu) opened by pressing
Help button in Find dialog. This page contains also a link to help page
Regular Expressions (Legacy) explaining the regular expression characters for the legacy regular expression engines UltraEdit and Unix. Another help page explains the most important Perl regular expression characters. The Perl engine is the most powerful engine, but is also the more difficult to learn because of the many options it offers.
Okay, now let us analyze the search string.
% matches the start of line. Indicates the search string must be at the beginning of a line but does not include any line terminator characters in the resulting string selected.
^(...
^) brackets or tags an expression to use in the replace command. A regular expression may have up to 9 tagged expressions, numbered according to their order in the regular expression. In this search string there is only one which is referenced in the replace string by
^1. There is also the power tip
tagged expressions explaining the technique of re-using a part of the found string in the replace string.
* matches any number of occurrences of any character except newline.
^p matches a newline (CR/LF) (paragraph) (DOS Files).
$ matches the end of line. Indicates the search string must be at the end of line but does not include any line terminator characters in the resulting string selected.
So the search string means: Start search at beginning of a line (
%), find any nummber of any characters in this line up to DOS line termination and remember this string for being used in the replace (
^(*^)^p), and find any nummber of any characters on next line up to end of line (
*$). If you execute once a Find with this UltraEdit regular expression string, you will see that it selects one line completely with the DOS line terminator and the next line up to end of line without the DOS line terminator.
The replace string simple references the found string selected on first line without the line terminator. As a result the replace deletes the DOS line termination of the first selected line and the string on second line and the DOS line termination on second line becomes the DOS line termination of first line. That's it. So this replace simple selects two lines and deletes the second line.
For deleting all even lines this regular expression replace all command is executed from top of the file keeping all odd lines and deleting all even lines. For deleting all odd lines the same regular expression replace all command is used to delete every second line, but by executing it now beginning from second line, all odd lines are deleted except the first at top of the file which is finally deleted with an additional command.
To keep just every 5th line and delete all others you can use
InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNumGt 1
InsertLine
IfColNumGt 1
DeleteToStartofLine
EndIf
EndIf
Top
Find RegExp "%*^p*^p*^p*^p^(*^)$"
Replace All "^1"
There is just one problem with that macro respectively the search string. If the file has for example 13 lines, the file contains correct the lines 5 and 10, but wrongly also the lines 11 to 13 after running this macro. It would be necessary to do something to find out which line remains to delete them also with a second replace.
The following macro has the additional steps required to identify the remaining lines and delete them. The macro first inserts a character at start of every line which never does exist at start of a line. With the replace keeping only every Nth line this special marker character is removed from the lines which should be kept. The remaining lines can now be identified by the marker character and can be easily deleted.
InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNumGt 1
InsertLine
IfColNumGt 1
DeleteToStartofLine
EndIf
EndIf
Top
Find RegExp "%"
Replace All "»"
Find RegExp "%*^p*^p*^p*^p»^(*^)$"
Replace All "^1"
Find RegExp "%»*^p"
Replace All ""
Find RegExp "%»"
Replace All ""
You can see that I used the character
» as marker character. The last before regular expression replace all deletes the remaining lines at bottom of the file, the last regular expression replace all deletes just the marker character
» appended at end of the file.
But it can be annoying to always need to modify the macro to use it for a variable line number. Therefore I converted the macro codes to a script code. The script below asks the user which Nth lines to keep and deletes all other lines using the best method.
Code: Select all
if (UltraEdit.document.length > 0) { // Is any file opened?
// Ask user which Nth lines to keep. All others are deleted.
var nLineNum = UltraEdit.getValue("Enter number of every Nth line to keep:",1);
if (nLineNum > 0) {
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
UltraEdit.activeDocument.hexOff();
UltraEdit.activeDocument.bottom();
UltraEdit.ueReOn();
// Define line terminator type by default for DOS. If the version of
// UE/UES has support for the line terminator property, use it to set
// the line terminator string according to the value of this property.
var sLineTerm = "^p";
var bMacWorkaround = false;
if (typeof(UltraEdit.activeDocument.lineTerminator) == "number") {
if (UltraEdit.activeDocument.lineTerminator == 1) {
sLineTerm = "^n"; // UNIX file not converted to DOS.
}
else if (UltraEdit.activeDocument.lineTerminator == 2) {
// sLineTerm = "^r";
// Just using ^r should work, but depending on version of UE
// or UES the result could be that just the first Nth - 1 lines
// block at top of the file is deleted or just the first line.
// A workaround is to convert the file temporarily to DOS.
UltraEdit.activeDocument.unixMacToDos();
bMacWorkaround = true;
}
}
// Make sure that file ends with a line termination.
if (UltraEdit.activeDocument.isColNumGt(1)) {
UltraEdit.activeDocument.insertLine();
if (UltraEdit.activeDocument.isColNumGt(1)) {
UltraEdit.activeDocument.deleteToStartOfLine();
}
}
// Define the parameters for all regular expression replaces
// done with the UltraEdit regular expression engine.
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
// To avoid a script execution error on UltraEdit
// versions not supporting searching in columns.
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
UltraEdit.activeDocument.findReplace.searchInColumn=false;
}
// To delete all odd lines, use a simplified method deleting every
// second line beginning from line 2 and then delete the first line.
if (nLineNum == 2) {
UltraEdit.activeDocument.gotoLine(2,1);
UltraEdit.activeDocument.findReplace.replace("%^(*^)"+sLineTerm+"*$", "^1");
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.deleteLine();
}
// Deleting all even lines can be made very simple.
else if (nLineNum == 1) {
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.replace("%^(*^)"+sLineTerm+"*$", "^1");
}
// Keep just every Nth line is more complicated because remaining
// lines at bottom of the file must be identified and deleted too.
else {
// Build the regular expression search string to
// delete all lines except the line to keep.
var sRegExp = '*' + sLineTerm;
var sSearchExp = "%";
while (--nLineNum) sSearchExp += sRegExp;
sSearchExp += "»^(*^)$"
var_dump(sSearchExp);
UltraEdit.activeDocument.top();
// Insert the marker character » at beginning of every line.
UltraEdit.activeDocument.findReplace.replace("%", "»");
// Delete all lines except every Nth lines.
UltraEdit.activeDocument.findReplace.replace(sSearchExp, "^1");
// Delete all lines remaining at bottom of the file.
UltraEdit.activeDocument.findReplace.replace("%»*"+sLineTerm, "");
// Delete the marker character » appended to end of file.
UltraEdit.activeDocument.findReplace.replace("%»", "");
}
if (bMacWorkaround) UltraEdit.activeDocument.dosToMac();
}
}
Edit on 2011-08-31:
Script works now also for UNIX and MAC files not converted temporarily to DOS on opening the file if version of UltraEdit or UEStudio offers the lineTerminator property for active file.