R
R
realt2016-04-15 16:12:11
Command line
realt, 2016-04-15 16:12:11

How to delete lines using bat?

Hello. There is a hosts file. I need to delete 1, 2, 3, 4, 5, 6 lines from it, and then write lines with my own text instead. Help me please. How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-04-15
@realt

In general, it is better to delete not 1,2,3 ... lines, but using a type filter as in the sed post above, only using the standard findstr utility. If you give an example of the contents of the lines that need to be deleted, I will help write a filter.
If you still insist on 1,2,3, .... then you organize a loop where you read the file line by line, counting the lines and start writing from the desired line:

@echo off
setlocal enabledelayedexpansion
set "c=0"
del /f/q hosts1
for /f "tokens=* delims=" %%a in (hosts) do (
    set /a "c+=1"
    if !c! gtr 7 >>hosts1 echo.%%a
)
del /f/q host
copy add.txt+hosts1 hosts
del /f/q host1

Train not in the system directory. The script removes host. In the final script, write the full paths for the files. The add.txt file contains the lines to be added at the beginning of hosts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question