Find a line in a file and replace the next line

2019-02-27 16:36发布

问题:

Using a .bat script, I want to find a line that says # Site 1 and replace the text in the next line with a variable. I found tutorials on StackOverflow for finding and replacing a line, but not finding a line and replacing the next line. Any help?

回答1:

@echo off

set "the_file=C:\someFile"
set "search_for=somestring"
set "variable=http://site1"

for /f "tokens=1 delims=:" %%# in ('findstr /n  /c:"%search_for%" "%the_file%"') do (
    set "line=%%#"
    goto :break
)
:break


set /a lineBefore=line-1
set /a nextLine=line+1


break>"%temp%\empty"&&fc "%temp%\empty" "%the_file%" /lb  %lineBefore% /t |more +4 | findstr /B /E /V "*****" >newFile
echo %variable%>>newFile
more "%the_file%" +%nextLine% 1>>newFile

echo move /y newFile "%the_file%"

Check if newFile is ok and remove the echo at the front of the last line.

you need to set the three variables at the beginning by yourself.Have on mind that more command sets spaces instead of tabs



回答2:

@ECHO OFF
SETLOCAL
SET "filename=q28567045.txt"
SET "afterme=# Site 1"
SET "putme=put this line after # Site 1"
SET "skip1="
(
FOR /f "usebackqdelims=" %%a IN ("%filename%") DO (
 IF DEFINED skip1 (ECHO(%putme%) ELSE (ECHO(%%a)
 SET "skip1="
 IF /i "%%a"=="%afterme%" SET skip1=y
)
)>newfile.txt

GOTO :EOF

Produces newfile.txt

The flag `skip1 to skip the line is first reset, then the file is read line by line.

If the skip1 flag is set, then the replacement line is echoed in place of the line read; if not, the line read is echoed.

Then the skip1 flag is cleared

If the line read to %%a matches the string assigned to afterme then the flag skip1 is set (to y - but it doesn't matter what the value is)

Note that empty lines and those starting ; will be ignored and not reproduced - this is standard behaviour of for /f.

If you want to replce the starting file, then simply add

move /y newfile.txt "%filename%" 

before the goto :eof line.



回答3:

Even though I enjoy working with batch, I generally avoid using pure native batch to edit text files because a robust solution is usually complicated and slow.

This can be done easily and efficiently using JREPL.BAT - a hybrid JScript/batch utility that performs regular expression replacement. JREPL.BAT is pure script that runs natively on any Windows machine from XP onward.

@echo off
setlocal
set "newVal=Replacement value"
call jrepl "^.*" "%newValue%" /jbeg "skip=true" /jendln "skip=($txt!='# Site 1')" /f test.txt /o -

The /F option specifies the file to process

The /O option with value of - specifies to replace the original file with the result.

The /JBEG option initializes the command to skip (not replace) each line.

The /JENDLN option checks the value of each line just before it is written out, and sets SKIP off (false) if it matches # Site 1. The next line will be replaced only when SKIP is false.

The search string matches an entire line.

The replacement string is your value stored in a variable.



回答4:

This problem is similar to this one and may use an equivalent solution. The pure Batch file solution below should be the fastest one of its kind.

@echo off
setlocal EnableDelayedExpansion

set "search=# Site 1"
set "nextLine=Text that replaces next line"


rem Get the line number of the search line
for /F "delims=:" %%a in ('findstr /N /C:"%search%" input.txt') do set /A "numLines=%%a-1"

rem Open a code block to read-input-file/create-output-file

< input.txt (

   rem Read the first line
   set /P "line="

   rem Copy numLines-1 lines
   for /L %%i in (1,1,%numLines%) do set /P "line=!line!" & echo/

   rem Replace the next line
   echo %nextLine%

   rem Copy the rest of lines
   findstr "^"

) > output.txt

rem Replace input file with created output file
move /Y output.txt input.txt > NUL

This method will fail if the input file have empty lines and also have other limitations. For a further description of this method, see this post.