查找一个文件中的行并更换下一行 [英] Find a line in a file and replace the next line

查看:139
本文介绍了查找一个文件中的行并更换下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.BAT剧本,我想找到这样一行#网站1 并与一个变量的下一行替换文本。我发现教程上计算器用于查找和替换一条线,但没有找到一个线和替换的下一行。任何帮助吗?


解决方案

 关闭@echo设置the_file = C:\\ someFile
设置搜索内容= somestring
设置变量= HTTP:// site1的FOR / F标记= 1 delims =%%#在('FINDSTR / N / C:%搜索字符串%%the_file%')做(
    一套行= %%#
    转到:突破

:打破
集/年lineBefore =行-1
集/一个nextLine =行+ 1
打破>中的%temp%\\空&放大器;&安培; FC%temp%\\空%the_file%/磅%lineBefore%/ T |更多+4 | FINDSTR / B / E / V*****> NEWFILE
回声%变量%GT;> NEWFILE
更多的%the_file%+%nextLine%1>> NEWFILE回声移动/ Y NEWFILE%the_file%

检查 NEWFILE 是确定并删除回声在最后一行的前面。

您需要通过yourself.Have在心中年初设定的三个变量,更多的命令设置空格代替标签页

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?

解决方案

@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

这篇关于查找一个文件中的行并更换下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆