• Login
Community
  • Login

Remove last 5 characters from lines in text file?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 3 Posters 3.9k Views
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.
  • D
    drghty
    last edited by Jun 19, 2023, 12:44 PM

    Hi everyone,

    I would like to get Notepad++ to remove the last 5 lines from a text file I have. I want to turn this

    10 Cloverfield Lane 2016
    12 Angry Men 1957

    into this

    10 Cloverfield Lane
    12 Angry Men

    I have Googled but I’m stuck. Thanks for any help.

    M 1 Reply Last reply Jun 19, 2023, 2:27 PM Reply Quote 0
    • M
      M Andre Z Eckenrode @drghty
      last edited by Jun 19, 2023, 2:27 PM

      @drghty said in Remove last 5 characters from lines in text file?:

      Hi everyone,
      I would like to get Notepad++ to remove the last 5 lines from a text file I have.

      Make sure caret is at start of file, or at or before first line you want to be affected, or enable Wrap around
      Search Mode = Regular expression

      FIND: .{5}$
      REPLACE: [empty]

      .{5} matches any five characters
      $ matches the end of the line

      M 1 Reply Last reply Jun 19, 2023, 2:30 PM Reply Quote 1
      • M
        Mark Olson @M Andre Z Eckenrode
        last edited by Jun 19, 2023, 2:30 PM

        @M-Andre-Z-Eckenrode
        Slightly off, because you need to make sure that . doesn’t match newline.
        Correct find/replace is
        FIND: (?-s).{5}$ ((?-s) turns OFF . matches newline)
        REPLACE: [empty]

        M 1 Reply Last reply Jun 19, 2023, 2:49 PM Reply Quote 2
        • M
          M Andre Z Eckenrode @Mark Olson
          last edited by Jun 19, 2023, 2:49 PM

          @drghty

          By the way, you wrote “remove the last 5 lines from a text file”, but your example indicates removal of the last 5 characters from each line, so I assume the latter was correct.

          @Mark-Olson

          Ah, yes — my bad, and thanks much for the correction.

          For @drghty , just to make sure you understand why Mark’s correction is potentially important, depending on the total content of your text file, my FIND pattern .{5}$ would also match up to five consecutive newline sequences, if your file has multiple consecutive newlines, if it does not include (?-s), or you don’t have . matches newline disabled.

          1 Reply Last reply Reply Quote 2
          3 out of 4
          • First post
            3/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors