0

我有两个文本文件

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

我想要做的是复制file2中包含hello和hai的所有行并将其覆盖在file1中的那些行上,没有。行数可能相等也可能不相等。但所有 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 添加到第一个文件中的搜索中

4

3 回答 3

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
于 2013-05-11T18:45:16.370 回答
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

输出是:

第1行
线2
你好-海-5
你好七海
6-嗨-你好
你好
于 2013-05-11T10:27:24.530 回答
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 时,它会输出 file2 中包含这两个字符串的所有这些行。该searching标志用于确保 file2 行仅输出一次。

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

于 2013-05-11T11:33:44.070 回答