Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Delete an entire line if a specific word is found inside that line

    Help wanted · · · – – – · · ·
    3
    6
    2814
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Costin R
      Costin R last edited by

      Hi,

      Is it possible to build a regex to find/mark an entire line of text when a specific word (specifically “#EXTINF”) is found in that line, so to be able to replace that line with “” (nothing) i.e. in order to remove that line?

      I have a text with about 6.000 lines, and I need to remove a lot of them, the lines containing that set of characters.

      Thank you,

      1 Reply Last reply Reply Quote 0
      • AdrianHHH
        AdrianHHH last edited by

        I see two possibilities depending on exactly what is wanted.

        To remove the lines entirely, use the MARK tab from the search/replace dialogue (click Control+Alt+M), select “Bookmark line”, enter the seach string and click “Mark all”. Expect to see blue blobs at the left of the marked lines. Next use Menu => Search => Bookmark => Remove bookmarked lines.

        The question also says to replace the line with nothing which might mean changing those lines to be empty lines but not deleting them. For this you can use a replace command, select “Regular expression” and “Wrap around”, in the “Find what” enter ^.*xxx.*$ but change xxx to the string you want. Be aware that any meta chararacters used in regular expressions must be escaped. Leave the “Replace with” empty and click on “Replace all”.

        Alan Kilborn 1 Reply Last reply Reply Quote 3
        • Alan Kilborn
          Alan Kilborn @AdrianHHH last edited by

          @AdrianHHH 's regular expression is not totally safe and can match more than intended in certain circumstances (i.e., lines that do not contain xxx). I would change it by putting an (?-s) at the start.

          AdrianHHH 1 Reply Last reply Reply Quote 1
          • AdrianHHH
            AdrianHHH @Alan Kilborn last edited by

            @Alan-Kilborn You have me worried. I have used that sort of expression many times without issue. Could you please explain how or when it can match unwanted line?

            Alan Kilborn 1 Reply Last reply Reply Quote 0
            • Alan Kilborn
              Alan Kilborn @AdrianHHH last edited by

              @AdrianHHH said in Delete an entire line if a specific word is found inside that line:

              Could you please explain how or when it can match unwanted line?

              Oh, sorry. I should have done that.
              As your expression used the . character, it could be affected by the . matches newline checkbox. Since you didn’t specify the state along with your regex, it was an incomplete specification.

              If you add (?-s) at the start of your regex, it removes the need to specify that the . matches newline checkbox should be unticked.

              Similarly, if you add (?s) at the start of your regex, it removes the need to specify that the . matches newline checkbox should be ticked.

              Start with this data:

              abc
              xxx
              abc
              xxx
              

              First, try (?-s)^.*xxx.*$ and see that it works as you intend (for OP’s need).

              Next, try (?s)^.*xxx.*$ and see that it matches more than intended.

              All I was trying to say was that if OP accidentally had the . matches newline box ticked from some prior operation, your regex would match unintended data. I could have said it better the first time around, sorry.

              1 Reply Last reply Reply Quote 2
              • AdrianHHH
                AdrianHHH last edited by

                Thank you @Alan-Kilborn for that explanation. I know and use the dot-matches-newline, and yes I should have specified it in my answer.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Copyright © 2014 NodeBB Forums | Contributors