2

I'm looking for a windows software which can bulk 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 windows software for this?

3
  • 1
    Please note that asking for software to do X is on-topic at this site but asking how to do something with software (Can I do this with Notepad++) is off-topic - you should direct that specific question at SuperUser.
    – Tom
    Apr 22, 2016 at 3:47
  • 2
    That wasn't my only question. I asked for a software and if notepad++ can do this and somebody knows how to do it, I don't think that creates any problem for anybody. Apr 22, 2016 at 3:50
  • Linked Discussion
    – Tom
    Apr 22, 2016 at 4:30

3 Answers 3

2

GnuWin is able to do it, e.g. sed. At the command line, type

for %f in ("*.txt") do C:\...\sed -i "1,3!d" "%f"

The for command is part of the Windows command line. %f defines a variable to hold the file name. in ("*.txt") defines which files to process. do C:\...\sed tells Windows to run a command.

-i will perform in-place operation, i.e. write to the same file. 1,3 defines a range of lines, ! chooses all other lines, d deletes them and "%f" passes the file to be processed to SED, so it knows which file to process.

GnuWin runs on Windows and is free.

1
  • Consider using -i.bak which will create a backup of all files before changing them (with the .bak extenstion).
    – Seth
    Apr 22, 2016 at 14:53
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.

0

There are lots of ways to do this using basic Unix/Linux tools.

I'd recommend to use awk in your case. To run awk on windows, you can use a Windows port of awk.

My personal experience is the it pays quickly to learn awk if you have to do text processing from time to time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.