将文本文件中的行复制到另一个文件

时间:2013-05-11 08:37:47

标签: batch-file cmd

我有两个文本文件

file1有以下几行

line1
line2
hello-hai-1
hello-2-hai
3-hai-hello
hello

和文件2有

line4
line3
hello-hai-5
hello-7-hai
6-hai-hello
hai
hello-4

我想要做的是复制包含文件2中的hello和hai的所有行,并在file1中的那些行上覆盖它,no。线条可能相同也可能不相等。但所有hello-hai线都在两个文件中

我使用的当前代码是

setlocal enabledelayedexpansion
for /f "tokens=1*delims=:" %%i in ('^<file2 essentials\findstr.exe /n "hello"') do set "#%%i=%%j"
(for /f "delims=" %%i in (file1) do (
    set "line=%%i"
    if not "!line!"=="!line:hello=!" (
    if not "!line!"=="!line:hai=!" (
        if not defined flag (
            for /f "tokens=1*delims==" %%a in ('set "#"') do echo(%%b
            set "flag=true"
        )
        ) else echo !line!
    )  else echo(!line!
))>output.txt 

这会将所有hello行复制到file1中的hello-hai行,我想知道如何在第一个文件中将hai这个词添加到搜索中

3 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

rem Find lines with both strings in file2
set i=0
for /F "delims=" %%a in ('findstr "hello" file2 ^| findstr "hai"') do (
   set /A i+=1
   set "file2[!i!]=%%a"
)

rem Merge file1 with the found lines in file2
set i=0
(for /F "delims=" %%a in (file1) do (
   set "line=%%a"
   if "!line:hello=!" neq "!line!" (
      if "!line:hai=!" neq "!line!" (
         set /A i+=1
         for %%i in (!i!) do echo !file2[%%i]!
      ) else (
         echo !line!
      )
   ) else (
      echo !line!
   )
)) > output.txt

编辑添加了新版本

以前的解决方案实现了逐行替换,即使匹配行不在任何文件中(文件合并),也可以使用,但要求匹配行的数量相同两个文件。下面新的更简单的版本按照OP要求的方式工作:

@echo off
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (file1) do (
   set "line=%%a"
   if "!line:hello=!" neq "!line!" (
      if "!line:hai=!" neq "!line!" (
         if not defined flag (
            findstr "hello" file2 | findstr "hai"
            set flag=true
         )
      ) else (
         echo !line!
      )
   ) else (
      echo !line!
   )
)) > output.txt

答案 1 :(得分:0)

试试这个:

@echo off &setlocal
set "tf1=%temp%\~%random%1"
set "tf2=%temp%\~%random%2"
set "tf3=%temp%\~%random%3"
set "tf4=%temp%\~%random%4"
set "pt=hello"
set "pu=hai"
<file1 findstr /lin "%pt%">"%tf1%"
<"%tf1%" findstr /li "%pu%">"%tf2%"
<file2 findstr /li "%pt%">"%tf3%"
<"%tf3%" findstr /li "%pu%">"%tf4%"
for /f "usebackqdelims=:" %%i in ("%tf2%") do (
    if not defined st set "st=%%i"
    set "fi=%%i"
)
set /a st-=1
<file1 (
for /l %%i in (1,1,%st%) do (
    set "line="
    set/p "line="
    setlocal enabledelayedexpansion
    echo(!line!
    endlocal
))>"%tf1%"
<file1>"%tf3%" more +%fi%
copy /a "%tf1%"+"%tf4%"+"%tf3%"=output.txt>nul
del "%temp%\~*"
type output.txt

输出是:

line1
line2
hello-hai-5
hello-7-hai
6-hai-hello
hello

答案 2 :(得分:0)

@ECHO OFF
SETLOCAL
SET searching=Y
(
FOR /f "delims=" %%i IN (file1.txt) DO (
 ECHO %%i|FIND "hello"|FIND "hai" >NUL
 IF ERRORLEVEL 1 (ECHO(%%i) ELSE (
  IF DEFINED searching TYPE "file2.txt"|FIND "hello"|FIND "hai"
  SET "searching="
 )
)
)>output.txt
TYPE output.txt
GOTO :EOF

这是一个没有临时文件的版本。它只是从file1再现不包含目标字符串BOTH的行。当在同一行中找到BOTH时,它会输出file2中包含两个字符串的所有行。 searching标志用于确保file2行仅输出一次。

它不会重现以;开头的空白行或行。如果需要,可以这样做。