1

I'm looking for a way so I can delete lines in text files after a specific line.

for example, a file contains 5 lines. I want to delete lines after the third line. see,

  • Line 1
  • Line 2
  • Line 3
  • xxxxxx
  • xxxxxx1

here, I want to keep first 3 lines(line 1, line 2, line 3) and delete remaining 2 lines(xxxxxx, xxxxxx1) and automatically save that text file. I want to do this to multiple text files. Want to delete every line after the 3rd line in every text file inside a folder.

Any way to do this using notepad++?? Thanks in advance

11
  • 2
    Hello again, the way your question was written here is fine and asking here how to accomplish this task with Notepad++ are both on-topic respectively, however, asking additionally for software recommendations here is off-topic at SuperUser.
    – Tom
    Apr 22, 2016 at 4:03
  • 1
    This may be a bit confusing but on SE we try to narrow down questions so again asking for Software Recommendations belongs on Software Recommendations and asking for instructions on how to use software to complete a task is (in most cases including this one) on-topic at SuperUser. Asking a question that includes both of these questions on one site is off-topic as a part of it does not belong on the site.
    – Tom
    Apr 22, 2016 at 4:06
  • 2
    I try and stay active on both SuperUser and Software Recommendations. I saw that there were two duplicate questions from what looked to be the same user - no, I'm not following you.
    – Tom
    Apr 22, 2016 at 4:11
  • 1
    I edited your question removing the part, "or any way to do this using notepad++?" which then made it on-topic on Software Recommendations. Again, your original question really including two questions, one of them making it off-topic. You can undelete your answer on Software Recommendations, it's now on-topic and if you remove the, "Any windows software for this?" part here on SuperUser- it will be on-topic here.
    – Tom
    Apr 22, 2016 at 4:16
  • 1
    This question is off-topic because you are asking us to suggest software that can do what you want. If you restructure your question to ask how to do what you want with a specific software it can stay. Otherwise, it will be closed as off-topic. Apr 22, 2016 at 4:41

2 Answers 2

2

This seems like quite standard text operation for a number of Linux tools (don't mind the technical incorrectness), GNU sed being one of them. Until Bash/Ubuntu comes out for Windows 10, you can use something like git bash which I tested to work for the below command.

sed -i '4,$d' *

Would delete all lines starting from the 4th line in all files in the directory you are in.

1
  • Hello, thanks a lot for this. It worked but there's an issue. After it delete lines, previous lines are added together. If there's a paragraph or space between first lines, those are gone after doing this. I want to keep all lines/rows. How to do it? please help Jun 11, 2016 at 3:26
1

This can be done without installing software but instead creating a simple batch script. Here's a script that will do what you want:

@echo off > NEWFILE & setLocal enableDELAYedeXpansioN
set/p D=delete all after what line?
set N=
:main
for /f "tokens=* delims= " %%a in (file.txt) do (
set/a N+=1
if !N! leq !D! echo.%%a
) >> NEWFILE

Where file.txt is the actual file. When you execute this script, it will ask you what line to delete after, simply say 3. Copy the above code and paste it in a .bat file.

1
  • Thanks a lot but i want to do this deletion inside folders and sub folders. want this deletion in every file inside lot of folders. Not file by file.. So can i do it by this method? What changes should i make? thanks in advance. Jun 11, 2016 at 3:29

You must log in to answer this question.

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