我想在 .txt 中搜索一个字符串。
确定行后需要在行尾添加另一个字符串...
请帮忙
例子:
(
echo 3-Trip-7000-23
echo 6-Lunch-600-2
echo 7-Breakfast-15-5
) > FILE.txt
如果我键入Lunch
它会在这种情况下打印,Lunch-600-2
因为在文件中搜索字符串“Lunch”,我该如何编写代码......我在 Windows 中
我想在 .txt 中搜索一个字符串。
确定行后需要在行尾添加另一个字符串...
请帮忙
例子:
(
echo 3-Trip-7000-23
echo 6-Lunch-600-2
echo 7-Breakfast-15-5
) > FILE.txt
如果我键入Lunch
它会在这种情况下打印,Lunch-600-2
因为在文件中搜索字符串“Lunch”,我该如何编写代码......我在 Windows 中
在顶部设置变量,我这样做是为了自动构建反编译框架的 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%"