Board logo

标题: [已解决]如何在.inf文本某两行之间插入一行数据 [打印本页]

作者: suntb     时间: 2008-3-7 16:52    标题: [已解决]如何在.inf文本某两行之间插入一行数据
假如test.inf内容如下,如何在某两行之间插入一行数据?

比如说在
 AddReg_1-2
 AddReg_1-3
中间插入一行
 AddReg_1-4
使小节内容变成
 AddReg_1-1
 AddReg_1-2
 AddReg_1-4
 AddReg_1-3



Version_1
Version_2
Version_3


AddReg_1-1
AddReg_1-2
AddReg_1-3


DelReg_1
DelReg_2
DelReg_3


Strings_1
Strings_2
Strings_3


Last edited by suntb on 2008-3-8 at 01:57 PM ]

作者: suntb     时间: 2008-3-7 16:56
或者也可以理解成要在指定行
AddReg_1-2
下面插入一行数据
AddReg_1-4

作者: terse     时间: 2008-3-7 18:31
@echo off
for /f "delims=" %%i in (test.inf) do (
echo %%i
if "%%i"=="AddReg_1-2" echo AddReg_1-4
)
pause

作者: 523066680     时间: 2008-3-7 18:49
不明白……

作者: suntb     时间: 2008-3-7 19:00
Originally posted by terse at 2008-3-7 18:31:
@echo off
for /f "delims=" %%i in (test.inf) do (
echo %%i
if "%%i"=="AddReg_1-2" echo AddReg_1-4
)
pause



terse兄的代码测试无反应 文本没有任何变动

作者: abcd     时间: 2008-3-7 19:08
terse兄的代码本来就没有改动inf文件,

仅仅是显示而已

作者: suntb     时间: 2008-3-7 19:18
Originally posted by abcd at 2008-3-7 19:08:
terse兄的代码本来就没有改动inf文件,

仅仅是显示而已


可是啥都没显示 只有pause的任意键提示

搜索了下原来帖子 在一个帖中willsort曾经给过一段代码
不过部分是乱码了 无法运行 哪位兄弟能帮忙还原下吗?

现将帖子http://www.cn-dos.net/forum/viewthread.php?tid=15788
第7楼willsort版主的回复贴在下面


:: Insert.bat - 在文本文件中的指定行插入一行
:: Will Sort - 2005/06/05 - CMD@WinXP
@echo off
for /f %%t in (1.txt) do echo %%t >>2.txt && if "%%t"=="4" echo insert_string>>2.txt


Last edited by suntb on 2008-3-7 at 07:19 PM ]

作者: terse     时间: 2008-3-7 20:01
Originally posted by suntb at 2008-3-7 19:00:



terse兄的代码测试无反应 文本没有任何变动

这样呢? 记得备份原文件
@echo off
for /f "delims=" %%i in (test.inf) do (
>>test_tmp.tmp echo %%i
if "%%i"=="AddReg_1-2" >>test_tmp.tmp echo AddReg_1-4
)
move /y "test_tmp.tmp" "test.inf" 2>NUL
pause

作者: abcd     时间: 2008-3-7 20:42
难道inf文件是UNICODE的??

作者: ceii     时间: 2008-3-7 23:49
用sed最简洁: sed -i "/AddReg_1-2/aAddReg_1-4" test.inf

作者: HAT     时间: 2008-3-8 00:19

:: Insert.bat - 在文本文件中的指定行插入一行
:: Will Sort - 2005/06/05 - CMD@WinXP
@echo off
for /f %%t in (1.txt) do echo %%t >>2.txt && if "%%t"=="4" echo insert_string>>2.txt

作者: DC2008     时间: 2008-3-8 02:04
哈哈,初到贵地,没想到是块我找了很久的宝地
我是一个新手,兴会兴会

作者: suntb     时间: 2008-3-8 13:36
Originally posted by HAT at 2008-3-8 00:19:
@echo off
for /f %%t in (1.txt) do echo %%t >>2.txt && if "%%t"== ...



还是不行 估计是遇到.inf中的符号无法处理了

用more转下测试通过 不知道是否有其他方法可以不用more

@echo off
for /f %%i in ('more "test.inf"') do (
echo %%i >>temp.txt
if "%%i"=="AddReg_1-2" echo AddReg_1-4>>temp.txt
)


Last edited by suntb on 2008-3-8 at 01:53 PM ]

作者: 523066680     时间: 2008-3-8 18:45
dos是万能的!他甚至不只是万能!

作者: nipo     时间: 2008-5-10 01:43
Originally posted by suntb at 2008-3-8 01:36 PM:



还是不行 估计是遇到.inf中的符号无法处理了

用more转下测试通过 不知道是否有其他方法可以不用more

@echo off
for /f %%i in ('more "te ...


这个好用。

作者: zxca112     时间: 2008-5-22 11:49
同问

作者: YoDe     时间: 2008-7-16 01:43
学习了!!!呵呵 谢谢前辈们