如何在.txt中搜索字符串?

时间:2016-03-31 00:33:40

标签: string batch-file search txtextcontrol

我想在.txt中搜索一个字符串。

识别出需要在行尾添加另一个字符串的行...

请帮助

示例:

(
echo 3-Trip-7000-23 
echo 6-Lunch-600-2 
echo 7-Breakfast-15-5
) > FILE.txt

如果我键入Lunch,则会打印Lunch-600-2,因为在文件中搜索字符串“Lunch”,我该怎么编码...我在Windows中

1 个答案:

答案 0 :(得分:0)

在顶部设置变量,我将其设置为自动化反编译框架的rom构建

@echo off
setlocal enableextensions enabledelayedexpansion
set search=search for this string
set file=searchfile.txt
set addstring=add this string to the line

set count=0
for /F "usebackq tokens=*" %%A in ("%file%") do (
set /A count+=1
Echo.%%A | findstr /C:"%search%">nul && (set magicnumber=!count!)
)
set count=0
for /F "usebackq tokens=*" %%A in ("%file%") do (
set /A count+=1
if !count! == %magicnumber% echo %%A%addstring% >>tmp.txt
if !count! == %magicnumber% set line=%%A
if not !count! == %magicnumber% echo %%A >>tmp.txt
)
del /Q %file%
ren "tmp.txt" "%file%"
echo "%search%" is on line %magicnumber% and line has been changed to "%line%%addstring%"