How to remove all lines before a string value including the line with a string



  • Hi,

    I am looking for a way to have Notepad ++ remove all the lines at the beginning of text files that come before a string, as well as that line. Example:

    In the list below, representing the contents of a text file, I would like to remove all lines before the word blah, as well as that line. How can I do it? I need to do it to a folder full of such files.

    There is a blank line after that text as well that I would like to remove, as in the example. I would like the result to start with “more stuff” on the first line of the resulting text file. There are other blank lines above blah, but blah is always present to trigger on.

    test1
    test2
    testing
    Hi there
    blah

    more stuff
    more stuff even
    etc
    etc
    My dog has fleas



  • -Place the cursor at the begining of the file and try the following regex (only tested in the example you provided):

    Search: (?s).*(?-s)^blah.*\R{2}
    Replace: [Leave it in blank]
    

    Once you are sure that it removes the unwanted lines, apply it to the other files.

    Hope this helps.



  • @Sofistanpp said in How to remove all lines before a string value including the line with a string:

    (?s).(?-s)^blah.\R{2}

    Thanks for that! It didn’t work for me, but here is the actual stuff I am trying to work with. What I need to do is key on a line that reads:

    Content-Type: message/rfc822

    The line will always be the same. RFC822 appears before this in the error message, so the phrase has to be a little more specific than just using that. Triggering on that whole line is fine, or some part of it that is unique would be ok. I think just using:

    message/rfc822

    would be ok.

    So I need to remove that line and everything above it, and the blank line below it.

    I would post more of the content but the site is marking it as spam. Bad BAD me… :/



  • (?s).*(?-s)^blah.*\R{2}
    It didn’t work for me,

    Of course blah won’t work if you’re searching for Content-Type: message/rfc822. Did you try for Content-Type: message/rfc822? Because (?s).*(?-s)^Content-Type: message/rfc822.*\R{2} works for me:

    4ba0c0bf-2fdd-448e-88c4-a99f450e6551-image.png

    If you’re trying a substring, like just message/rfc822, then you cannot use the ^ to anchor to the start of the line; or, if you want to keep the anchor, you need to consume the text between, like with (?s).*(?-s)^.*message/rfc822.*\R{2}



  • @PeterJones said in How to remove all lines before a string value including the line with a string:

    (?s).(?-s)^Content-Type: message/rfc822.\R{2}

    I had just tried with rfc822 and it did not work, but I have tried again with the string you gave me and it works. I really appreciate your help!

    Mark


Log in to reply