delete lines that have a number below a certain threshold

Help with writing and playing macros

delete lines that have a number below a certain threshold

Postby skeletrooper » Tue Oct 09, 2007 10:55 pm

Hi,

I have a large text file that has 3 numbers per line (latitude, longitude, elevation), space-delimited. Example rows are:
-75.00000 42.50000 518
-74.99583 42.50000 505
-71.40000 40.70417 -60

The first 2 numbers stay in the -75 & 42 range. I need to DELETE all lines where the 3rd value is less than -1500. I can handle string replacement OK, but I'm not sure about numbers.

Any ideas?

-Wes
User avatar
skeletrooper
Newbie
 
Posts: 3
Joined: Mon Oct 08, 2007 11:00 pm

Re: delete lines that have a number below a certain threshol

Postby Mofi » Wed Oct 10, 2007 8:57 am

Following macro should do the job.

The first replace deletes all lines with third number less than -10000.
The second replace deletes all lines with third number in range of -2000 to -9999.
The third replace deletes all lines with third number in range of -1500 to -1999.

The macro property Continue if a Find with Replace not found or Continue if search string not found must be checked for this macro.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
TrimTrailingSpaces
Find RegExp "%* * -[1-9][0-9][0-9][0-9][0-9]+^p"
Replace All ""
Find RegExp "%* * -[2-9][0-9][0-9][0-9]^p"
Replace All ""
Find RegExp "%* * -1[5-9][0-9][0-9]+^p"
Replace All ""

Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: delete lines that have a number below a certain threshol

Postby skeletrooper » Mon Dec 10, 2007 12:39 am

Hi Again,

How about if I want to replace the third number with a value of -2000, instead of deleting the line (the rest of the conditions the same)...something like this?

Find RegExp "%* * -[1-9][0-9][0-9][0-9][0-9]+^p"
Replace All "^1 ^2 -2000"
Find RegExp "%* * -[2-9][0-9][0-9][0-9]^p"
Replace All "^1 ^2 -2000"
Find RegExp "%* * -1[5-9][0-9][0-9]+^p"
Replace All "^1 ^2 -2000"

Thanks again!
User avatar
skeletrooper
Newbie
 
Posts: 3
Joined: Mon Oct 08, 2007 11:00 pm

Re: delete lines that have a number below a certain threshol

Postby Mofi » Mon Dec 10, 2007 8:58 am

You have forgotten the round brackets to tag the string part which should not be modified by the replaces.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
TrimTrailingSpaces
Find RegExp "%^(* * -^)[1-9][0-9][0-9][0-9][0-9]+^p"
Replace All "^12000"
Find RegExp "%^(* * -^)[2-9][0-9][0-9][0-9]^p"
Replace All "^12000"
Find RegExp "%^(* * -^)1[5-9][0-9][0-9]+^p"
Replace All "^12000"
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: delete lines that have a number below a certain threshol

Postby skeletrooper » Thu Dec 20, 2007 1:05 am

Last question - and this I'm not sure can be done in UltraEdit. I realize what I now need is Perl or Javascript. But I'll ask anyway...

Is it possible to add 2000 to the 3rd number? (My range for the 3rd number is 2000.0 to -2000.0)

For example:
-70.098333 37.823333 -1956.1
becomes:
-70.098333 37.823333 43.9

Mofi, thank you in advance...you really saved my hide.
User avatar
skeletrooper
Newbie
 
Posts: 3
Joined: Mon Oct 08, 2007 11:00 pm

Re: delete lines that have a number below a certain threshol

Postby jorrasdk » Thu Dec 20, 2007 8:31 am

skeletrooper wrote:Last question - and this I'm not sure can be done in UltraEdit. I realize what I now need is Perl or Javascript.

You haven't mentioned what UE version you use - but since you mention javascript, are you possibly using version 13 and above ?

In a javascript it would be pretty simple, though not necessarily "macro fast".
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: delete lines that have a number below a certain threshol

Postby jorrasdk » Thu Dec 20, 2007 10:40 am

Well I decided not to wait for you answer about your UE version as I needed a small "coffee break challenge" ;-)


Below you'll find a script that will work for UE version 13 and above that adds 2000 to the last number.

If you still need some of the macro tasks performed on your data (delete lines and stuff) before adding 2000 remember to run these macros before or try and incorporate the same logic into the script.

Code: Select all
// Support function: remove starting and ending / as UE does not recognize these for regexp
RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); };

// First ensure last line ends with CRLF
UltraEdit.activeDocument.bottom();
var ColNum = UltraEdit.activeDocument.currentColumnNum;
if (typeof(UltraEdit.activeDocumentIdx) == "undefined") ColNum++;
if (ColNum>1) {
   UltraEdit.activeDocument.insertLine();
}

// First go to top of document
UltraEdit.activeDocument.top();

UltraEdit.perlReOn(); /* Let's use Perl regexp syntax */
UltraEdit.activeDocument.findReplace.regExp = true;

//Find lines like:
//-70.098333 37.823333 -1956.1
//We use 2 tagged expressions grouping like this (shown with [] pairs)
//[-70.098333 37.823333 ][-1956.1]
var regexpFind = /^(.*? .*? )(.*?)$/;

// Loop and search for pattern
while ( UltraEdit.activeDocument.findReplace.find(regexpFind.toUEregexp()) ) {
   // get line
   var line = UltraEdit.activeDocument.selection;

   // now parse into variables with perl regexp
   var [, wLineStart,wNumber] = regexpFind.exec(line);

   // Add 2000 to number (toFixed because of the buggy Math in javascript)
   wNumber = (Number(wNumber) + 2000).toFixed(1);

   // Write result back on top of the selected line:
   UltraEdit.activeDocument.write(wLineStart+String(wNumber));
}

// reposition to top
UltraEdit.activeDocument.top();
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark


Return to Macros