Deleting multiple lines that contain a certain text



  • Hi there, I want to delete for example, the whole 7 lines that contain the word Guest01 from <passthrumac> to </passthrumac>. I have thousands of these entries in a XML document. I want to remove all those entries with Guest01. From <passthrumac> to </passthrumac> with Guest01 inside of it. Is this possible? thank you so much!

    	<passthrumac>
    		<action>pass</action>
    		<mac>00:11:22:33:44:55</mac>
    		<ip>10.10.50.254</ip>
    		<username>Guest01</username>
    		<descr><![CDATA[Auto-added for user Guest01]]></descr>
    	</passthrumac>


  • I think the following regex will find those instances where Guest01 exists in the group of lines.
    As I wasn’t sure if there were actually spaces at the start of the lines I have incorporated spaces of any length.

    Find what: (?s)^\h+?<passthrumac>.+?Guest01.+?</passthrumac>\R
    Replace with: empty field here <-- this means nothing in this field

    I hope this meets your needs.

    Terry



  • Thanks for the quick reply!

    I just tried your code but it highlights everything when I do find what:

    Do I check on Match case, Wrap around as well? Regular expression with checked Matches newline?

    Here’s an example of the text before and after. Thousands of other entries with different names of course.
    <passthrumac>
    <action>pass</action>
    <mac>44:55:66:77:88:99</mac>
    <ip>10.10.50.252</ip>
    <username>randomperson2</username>
    <descr><![CDATA[Auto-added for user randomperson2]]></descr>
    </passthrumac>
    <passthrumac>
    <action>pass</action>
    <mac>11:22:33:44:55:66</mac>
    <ip>10.10.50.254</ip>
    <username>Guest01</username>
    <descr><![CDATA[Auto-added for user Guest01]]></descr>
    </passthrumac>
    <passthrumac>
    <action>pass</action>
    <mac>66:55:44:33:22:11</mac>
    <ip>10.10.50.253</ip>
    <username>randomperson1</username>
    <descr><![CDATA[Auto-added for user randomperson1]]></descr>
    </passthrumac>

    I’ve had a friend make this for me, but it does not seem to work for me. It highlights the whole document.

    <passthrumac>((.|\n))guest01((.|\n))passthrumac>$



  • You don’t need to tick match case, unless the Guest01 may sometimes be guest01.

    I do see the problem though. In your NEW example you have Guest01 mentioned, but as "Auto-Added for user Guest01. That’s why my regex grabbed it as well. I assumed Guest01 would only exist once inside the group of lines if between the <username>/</username> tags.

    So the revised regex is
    Find what: (?s)^\h+?<passthrumac>.+?<username>Guest01</username>.+?</passthrumac>\R
    Same Replace what field.

    This is a regex which needs the search mode as “regular expression” and Wrap-around ticked. Match Case should not be unless as I mentioned above sometimes Guest01 is guest01.

    Have a try at this regex and come back with how it went this time.

    Terry



  • Actually cancel my last post, I didn’t test it as I assumed adding the new string to search on would work. I’ve now tested and it does NOT. It grabs the next group of lines as well.

    Stand by, either I or someone else will provide a better regex. I was also wrong in my answer in regards the Match case. I had it back to front.

    Terry



  • Right, this time I tested it and I think this regex might achieve the correct result. Sorry about the false start. My mind on other matters.

    Find what: ^<passthrumac(?:.+\R){4}.+?>Guest01<(?:.+\R){2}</passthrumac>\R
    Replace with: empty field here <— nothing in this field
    So search mode is “regular Expresssion” and “wrap-around” ticked, nothing else.

    You mentioned Match Case. You could tick it if you ONLY want exactly the text portions as typed in the Find what field, so <passthrumac>, rather than say <PassThruMAC>, and Guest01 rather than guest01. Otherwise I’d leave it unticked.

    Again, sorry, have a go with this and let me know.

    This regex expects EXACTLY 7 lines, hence I removed the (?s) from the first incorrect one. So the lines must be in the same order as you show. That’s why I only check for >Guest01< rather then look for <username>Guest01</username>. So it cannot confuse Guest01 on that line instead of the following Guest01 with the Auto-added string in front.

    Terry


Log in to reply