Question

I have a question related to replacing a certain value in a text file.

My text file contains:

 x              y                  z
 979999.088252  994114.506680      -1
 979999.053624  995441.988245      -4
 979999.495066  905496.233609      -3
 979999.380029  905467.292380      0
 979999.338490  905554.964370      5
 979999.910165  905624.191735      2

I want to replace the values that greater than 2 (z>2) with value of 2 (z=2).

I am using Notepad ++ .

Does anybody has an idea?

Thanks before

Was it helpful?

Solution

Choose the regular expression option in the search and replace and use [3-9]$| [1-9][0-9][0-9]*$ as the find what text and 2 as the replace with text (both of them have a leading space).

What the find what text says is: a space followed by a single digit number between 3 and 9 just before the end of the line OR a space followed by a two or more digit number just before the end of the line

It doesn't match minus signs so they won't be replaced. It doesn't match anthing with a decimal place (I don't know if you wanted that). The number has to be preceeded by a space (you will need to change it if you are using tabs instead). And there can't be any spaces/tabs after the number so it has to be at the end of the line.

Ok, to answer the question in the comment. If the data included floating point values with decimal points and you wanted to change anything bigger than 2 into a 2 you could use this [2-9](?:\.[0-9]+)?$| [1-9][0-9]+(?:\.[0-9]+)?$ as the find what text and 2 as the replace with text (both of them have a leading space).

The way that works may need a bit of explanation.

  • [0-9] matches any ASCII value between 0 and 9 (all the digits)
  • X+ matches 1 or more of X
  • [0-9]+ matches 1 or more [0-9] (a string of digits)
  • \. matches a literal period (it needs to be escapred with a \ since . means something else)
  • \.[0-9]+ matches a decimal point followed by a string of digits
  • (?:X) is a non capturing group that matches X - it's just a way to just group things together
  • (":\.[0-9]+) is a group containing a decimal point followed by a string of digits
  • X? matches 0 or 1 of X (making X optional)
  • (":\.[0-9]+)? is an optional group containing a decimal point followed by a string of digits
  • [2-9] matches any ASCII value between 2 and 9
  • [2-9](":\.[0-9]+)? matches any integer or floating point number between 2 and 9.9999999...
  • [2-9](":\.[0-9]+)? (with a leading space) matches any integer or floating point number between 2 and 9.9999999... that has a leading space
  • [2-9](":\.[0-9]+)?$ (with a leading space) matches any integer or floating point number between 2 and 9.9999999... that has a leading space that is at the end of a line
  • [1-9] matches any ASCII value between 1 and 9
  • [1-9][0-9]+ matches any integer from 10 up
  • [1-9][0-9]+(":\.[0-9]+)? matches any integer or floating point number from 10 up
  • [1-9][0-9]+(":\.[0-9]+)? (with a leading space) matches any integer or floating point number from 10 up with a leading space
  • [1-9][0-9]+(":\.[0-9]+)?$ (with a leading space) matches any integer or floating point number from 10 up with a leading space that is at the end of a line
  • X|Y matches X or Y
  • [2-9](":\.[0-9]+)?$| [1-9][0-9]+(":\.[0-9]+)?$ (with a leading space) matches any integer or floating point number between 2 and 9.9999999... or from 10 up with a leading space that is at the end of a line
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top