Windows 在文件中查找一行并替换下一行
使用.bat脚本,我想找到一行,上面写着Windows 在文件中查找一行并替换下一行,windows,batch-file,replace,Windows,Batch File,Replace,使用.bat脚本,我想找到一行,上面写着#Site 1,并用变量替换下一行中的文本。我在StackOverflow上找到了查找和替换一行的教程,但没有找到一行并替换下一行。有什么帮助吗 @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_fo
#Site 1
,并用变量替换下一行中的文本。我在StackOverflow上找到了查找和替换一行的教程,但没有找到一行并替换下一行。有什么帮助吗
@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%"
检查newFile
是否正常,并移除最后一行前面的echo
您需要在开始时自行设置这三个变量。请记住,更多的命令设置空格而不是制表符
SETLOCAL
设置“filename=q28567045.txt”
设置“afterme=#站点1”
设置“putme=将此行放在#站点1”之后
设置“skip1=”
(
对于/f“usebackqdelims=“%%a IN”(“%filename%”)DO(
如果定义了skip1(回显(%putme%)其他(回显(%a))
设置“skip1=”
如果/i“%%a”=%afterme%”设置skip1=y
)
)>newfile.txt
后藤:EOF
生成newfile.txt
首先重置“skip1跳过行”标志,然后逐行读取文件
如果设置了skip1
标志,则替换行被echo
ed代替读取的行;如果未设置,则读取的行被回显
然后清除skip1
标志
如果读取到%%a
的行与分配给afterme
的字符串匹配,则设置标志skip1
(设置为y
,但值是什么并不重要)
请注意,空行和以;
开头的空行将被忽略且不会复制-这是for/f
的标准行为
如果要回复起始文件,只需添加
move /y newfile.txt "%filename%"
在
goto:eof
行之前。尽管我喜欢使用批处理,但我通常避免使用纯本机批处理来编辑文本文件,因为健壮的解决方案通常复杂而缓慢
这可以通过使用一个混合的JScript/批处理实用程序轻松高效地完成,该实用程序执行正则表达式替换。JREPL.BAT是纯脚本,从XP开始在任何Windows机器上本机运行
@echo off
setlocal
set "newVal=Replacement value"
call jrepl "^.*" "%newValue%" /jbeg "skip=true" /jendln "skip=($txt!='# Site 1')" /f test.txt /o -
/F选项指定要处理的文件
值为-
的/O选项指定用结果替换原始文件
/JBEG选项初始化命令以跳过(而不是替换)每一行
/JENDLN选项在写入前检查每一行的值,如果与#Site 1
匹配,则设置SKIP off(false)。只有当SKIP为false时,才会替换下一行
搜索字符串与整行匹配
替换字符串是存储在变量中的值。此问题类似于并可能使用等效的解决方案。下面的纯批处理文件解决方案应该是同类解决方案中速度最快的
@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
@echo关闭
setlocal EnableDelayedExpansion
设置“搜索=#站点1”
设置“nextLine=替换下一行的文本”
rem获取搜索行的行号
对于('findstr/N/C:'%search%'input.txt')中的/F“delims=:”%%a,请设置/a“numLines=%%a-1”
rem打开代码块以读取输入文件/创建输出文件
output.txt
rem用创建的输出文件替换输入文件
移动/Y output.txt input.txt>NUL
如果输入文件有空行并且有其他限制,此方法将失败。
有关此方法的进一步说明,请参阅。那么每个变量的确切含义是什么?@AIDANEWARDS-您要在下一行设置的变量(搜索后)似乎无法使用此变量:set/p ip=“Insert server ip:”set“the_file=pc2v9.ini”set“search_For=”Site 1“set”variable=server=“%ip%”:50002”@AidanEdwards您是否检查了
newFile
的内容?它应该与脚本位于同一目录中。如果是,如何使其自动替换?