-1

I want remove everything after the word "last" in multiple text files so that it behaves like this:

Prior to running:

Text1
Text2
last
Text3
Text4

After running:

Text1
Text2

This can be accomplished with any of Windows compatible program (batch, python, etc.)

3
  • 1
    what have you tried and what research have you done? this isn't a code writing service.
    – mael'
    Jun 18, 2019 at 17:30
  • 1
    @mael' None of the answers here involve writing any more than two lines of code(and only one involves any code at all), so it's not like he is asking for a code writing service. And if it was a code question he'd ask on SO not SU. If he had tried to write his own code to do it and got stuck, it'd probably be overkill and reinventing the wheel.
    – barlop
    Jun 19, 2019 at 1:38
  • Your example shows no trailing newline after running, but the three answers so far result in one. I assume it's not an issue, right? Jun 19, 2019 at 12:23

3 Answers 3

3

Using Notepad++

  • Ctrl+H
  • Find what: last[\s\S]+
  • Replace with: LEAVE EMPTY
  • check Match case
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

last        # literally
[\s\S]+     # 1 or more any character, including linebreaks
2
  • [\s\S]+ or I guess [\w\W]+ would be the same? seems a really interesting way of doing .+ where a . matches new line box has been ticked. Good stuff
    – barlop
    Jun 19, 2019 at 1:42
  • @barlop: Thank you. Yes, [\s\S] <=> [\w\W] <=> [\d\D].
    – Toto
    Jun 19, 2019 at 8:19
2

in Python it's just read an input file into myString then write myString[:myString.find("last")] into the output file

see slice notation and string.find()

1
1

Simple answer: This regex will select everything after "last":

^last.*

When using regex in Notepad++, ensure that ". matched newline" is checked. You may then delete the selected part.

Not the answer you're looking for? Browse other questions tagged .