delete lines that have a number below a certain threshold

delete lines that have a number below a certain threshold

3

    Oct 10, 2007#1

    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

    6,686585
    Grand MasterGrand Master
    6,686585

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

      Oct 10, 2007#2

      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.
      Best regards from an UC/UE/UES for Windows user from Austria

      3

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

        Dec 10, 2007#3

        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!

        6,686585
        Grand MasterGrand Master
        6,686585

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

          Dec 10, 2007#4

          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"
          Best regards from an UC/UE/UES for Windows user from Austria

          3

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

            Dec 20, 2007#5

            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.

            262
            MasterMaster
            262

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

              Dec 20, 2007#6

              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".

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

                Dec 20, 2007#7

                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();