How can I merge two text files but only add the missing lines from the first file in a Windows batch file?

huangapple go评论77阅读模式
英文:

How can I merge two text files but only add the missing lines from the first file in a Windows batch file?

问题

Final.txt

15 绿色
15 黄色
8 蓝色
17 红色
14 粉色

(Note: I've translated the color names into Chinese for your reference.)

英文:

I have 2 txt files:

1.txt

15 green
8  blue
9  pink
12 red

2.txt

15 yellow
8 blue
17 red
14 pink

I want to Final.txt have all lines from 2.txt + only line from 1.txt if that line contain different color. So final.txt can have only one color. Order is not important.

Solution from @Mofi worked perfectly, after I remove /E as he suggested!

Final.txt

15 green
15 yellow
8 blue
17 red
14 pink

答案1

得分: 0

SETLOCAL
rem 以下设置用于目录和文件名,是名称
rem 以下设置用于目录的名称
rem 我用于测试的名称故意包含空格,以确保

rem 这个过程可以使用这种名称。这些需要根据您的情况进行更改。

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q76356312.txt"
SET "filename2=%sourcedir%\q76356312_2.txt"
SET "outfile=%destdir%\outfile.txt"

(
TYPE "%filename1%"
FINDSTR /x /v /g:"%filename1%" <"%filename2%"
)>"%outfile%"

GOTO :EOF

应按您的要求执行,提供您提供的数据,但不是按您提供的顺序。

请注意,第一个文件中写着“12 red”,而第二个文件中写着“17 red”。

英文:
SETLOCAL
rem The following settings for the directories and filenames are names
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure

rem that the process works using such names. These will need to be changed to suit your situation.

SET &quot;sourcedir=u:\your files&quot;
SET &quot;destdir=u:\your results&quot;
SET &quot;filename1=%sourcedir%\q76356312.txt&quot;
SET &quot;filename2=%sourcedir%\q76356312_2.txt&quot;
SET &quot;outfile=%destdir%\outfile.txt&quot;

(
 TYPE &quot;%filename1%&quot;
 FINDSTR /x /v /g:&quot;%filename1%&quot; &lt;&quot;%filename2%&quot;
)&gt;&quot;%outfile%&quot;

GOTO :EOF

Should do as you ask, providing the data that you provide, but not in the sequence you provide.

Note that the first file says "12 red" but the second has "17 red"

答案2

得分: 0

以下是代码的翻译部分:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileMain=2.txt"
set "FileResult=Result.txt"
set "FileSupplementary=1.txt"

if exist "%FileMain%" goto CheckSupplementary
echo 错误:未找到文件 "%FileMain%"。& goto ErrorPause

:CheckSupplementary
if exist "%FileSupplementary%" goto ProcessFiles
echo 错误:未找到文件 "%FileSupplementary%":ErrorPause
echo(
pause
exit /B 1

:ProcessFiles
(
    for /F "usebackq tokens=1*" %%I in ("%FileSupplementary%") do if not "%%J" == "" %SystemRoot%\System32\findstr.exe /E /L /M /C:"%%J" "%FileMain%" >nul || echo(%%I %%J
    type "%FileMain%"
)>>"%FileResult%"
endlocal

请注意,此翻译仅包括代码部分,不包括注释和链接。如果需要翻译注释和链接部分,请提供具体的文本。

英文:

There could be used the following batch code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set &quot;FileMain=2.txt&quot;
set &quot;FileResult=Result.txt&quot;
set &quot;FileSupplementary=1.txt&quot;

if exist &quot;%FileMain%&quot; goto CheckSupplementary
echo ERROR: File &quot;%FileMain%&quot; not found.&amp; goto ErrorPause

:CheckSupplementary
if exist &quot;%FileSupplementary%&quot; goto ProcessFiles
echo ERROR: File &quot;%FileSupplementary%&quot; not found.
:ErrorPause
echo(
pause
exit /B 1

:ProcessFiles
(
    for /F &quot;usebackq tokens=1*&quot; %%I in (&quot;%FileSupplementary%&quot;) do if not &quot;%%J&quot; == &quot;&quot; %SystemRoot%\System32\findstr.exe /E /L /M /C:&quot; %%J&quot; &quot;%FileMain%&quot; &gt;nul || echo(%%I %%J
    type &quot;%FileMain%&quot;
)&gt;&quot;%FileResult%&quot;
endlocal

The main for /F loop splits up each non-empty line in the file 1.txt not beginning with a semicolon into two substrings using normal space and horizontal tab as delimiters. The first substring being the number is assigned to the specified loop variable I and the rest of the line after the spaces/tabs after the number to the next loop variable J according to ASCII table.

FINDSTR is used to search case-sensitive and literally at end of the line for the string assigned to loop variable J which should be never an empty string verified before with a simple string comparison.

Note: False positives are possible if file 1.txt contains a line like 28 purple and file 2.txt contains a line like 20 brown and purple. The line 28 purple from 1.txt is in this case not in Result.txt because of there is a line in 2.txt also ending with a space and purple. The usage of a regular expression is not trivial without knowing which characters could be in a line read from file 1.txt after the number at beginning of the line.

The line read from file 1.txt is with a single normal space output if the string assigned to loop variable J cannot be found with a normal space before at end of a line in file 2.txt.

All the output lines from file 1.txt are written first to Result.txt on which are appended finally all the lines in file 2.txt.

The space character in /C:&quot; %%J&quot; and in echo(%%I %%J should be replaced by a horizontal tab character if the two input files have a horizontal tab character instead of one or more spaces between number and rest of the line (color name).

Please note further that a line with a color name may appear twice also in result file if the line has in first text file a trailing whitespace character while there is no trailing whitespace in the line with same color name in second text file or vice versa. It is possible to remove the FINDSTR option /E and remove the space/tab left to %%J in the search string defined with option /C: for making the find less restrictive by allowing everything left and right to the string to find in file 2.txt. But that can result in more false positives depending on real strings in the two text files.

Note: A trailing whitespace in file 1.txt after a color name and no trailing whitespace in file 2.txt on line with same color name would even without usage of option /E result in having the color name finally from both files in the result file.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • exit /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • setlocal /?
  • type /?

See also single line with multiple commands using Windows batch file for an explanation of the unconditional command operator &amp; and the conditional command operator ||.

Read the Microsoft documentation about Using command redirection operators for an explanation of &gt;nul.

huangapple
  • 本文由 发表于 2023年5月29日 17:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定