将特定行从一个文件复制到下一个文件

时间:2013-05-05 14:08:04

标签: cmd

问题-1)我有两个文件

file1有

abc=123
acd=234
helloa=455
hellob=768
adb=234

file2有

abc=123
acd=564
helloa=2343
hellob=123
adb=685

我知道字符串helloahellob,但我不知道它的右侧是什么, 我想要做的是,将file1的helloahellob右侧的值更改为file2的值。

我尝试了什么

set a=0
for /f "delims=" %%i in ('^<file1 findstr /n "^"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!"
if not "!line!"=="!line:helloa=!" set a=!line!
endlocal
)

然后替换file1中的行但是行永远不会被复制到a,a仍为0

问题-2)这个的延伸 file1有

line1
line2
hello1
hello2

file2的

line1
line2
hello1
hello2
hello3

这里我要删除file1中的所有hello行,并将其替换为file {2}行hello

1 个答案:

答案 0 :(得分:0)

这解决了Q1

@echo off

for /f "tokens=1,* delims==" %%a in ('find "helloa" ^< file2.txt') do set helloa=%%b
for /f "tokens=1,* delims==" %%a in ('find "hellob" ^< file2.txt') do set hellob=%%b

for /f "delims=" %%a in (file1.txt) do (
for /f "delims==" %%b in ("%%a") do (
if "%%b"=="helloa" (
>>file.tmp echo helloa=%helloa%
) else if "%%b"=="hellob" (
>>file.tmp echo hellob=%hellob%
) else (
>>file.tmp echo(%%a
)
)
)
move file.tmp file1.txt
pause
相关问题