Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - Find a line in a file and replace the next line

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
@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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...