How to delete specific lines only with macro
-
Hi,
I have a text file that lists georeferenced line in UTM format and should go like this: line number, coordinate of the first point and coordinate of the last point.
Example:
L106900
359088.16 4927710.64
372696.93 4924424.02But, when the file was made, it had generated a bunch of coordinates in between the first and last coordinates that I would like to get rid of. I started doing it manually but it’s thousands of lines I need to remove.
Any idea how to automate that?
Thanks,
D -
Hello, @dominic-leblanc and All,
Well, here is a possible solution , using a regular expression
Given this example of INPUT text :
L106900 359088.16 4927710.64 368425.29 4925689.13 359928.56 4926782.72 372696.93 4924424.02 L106901 359261.78 4932588.32 375624.69 4926854.61 359923.84 4926958.17 378952.70 4937532.29 396284.33 4925818.24 382621.65 4920105.59And the following regex S/R :
SEARCH/MARK
(?-is)^L\d+\R.+\R\K(?s:.*?)(?=.+(?:\R+L\d+|\R*\z))REPLACE
Leave EMPTYYou would be left, after execution, with this OUTPUT text :
L106900 359088.16 4927710.64 372696.93 4924424.02 L106901 359261.78 4932588.32 382621.65 4920105.59As you can see, this regex :
-
Keep the first line after a line beginning the uppercase letter
L -
Keep the last line before a line beginning the uppercase letter
Lor before the end of text / file -
Delete any coordinates line located in between
IMPORTANT :
-
You must select the
Regular expressionsearch mode -
You may check the
Wrap aroundoption, if you need to modify the whole text of current file -
You can match the different occurrences, using the Find dialog and the
Find Nextbutton -
You can see the bunch of all the lines to delete , using the Mark dialog and the
Mark Allbutton -
When using the Replace dialog, you must use, exclusively the
Replace Allbutton ( NOT theReplacebutton ) because of the\Ksyntax part of the regex search expression ! -
Each line beginning with an
Lletter and digits may be preceded or not with some empty lines -
The last bunch or coordinates lines may be followed or not with some empty lines
Best Regards,
guy038
P.S. :
If, by mistake, you re-run this regex search/replacement, you may be surprised of the message
Replace All: n occurrences were replaced .... That is quite normal, because it replaces, this time, the zero-length bunch of in-between lines with nothing again ! -
-
Wow! I’m impressed thank you! It didn’t work for some lines for which the number has a decimal though like L102300.1
-
Hi, @dominic-leblanc and All,
Ah…, OK ! As this fact wasn’t mentionned in your initial example, this case was not supposed to happen :-)
So, my new version of the search regex is simply :
- SEARCH/MARK
(?-is)^L[\d.]+\R.+\R\K(?s:.*?)(?=.+(?:\R+L[\d.]+|\R*\z))
Note that this version would also match if the initial georeferenced line was, for instance :
L.12.34.56.78.9I suppose that it’s not a problem if your text does not contain such lines ;-))
BR
guy038
- SEARCH/MARK
-
Thanks Guy, I really appreciate your help!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login