There could be used the following batch code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileMain=2.txt"
set "FileResult=Result.txt"
set "FileSupplementary=1.txt"
if exist "%FileMain%" goto CheckSupplementary
echo ERROR: File "%FileMain%" not found.& goto ErrorPause
:CheckSupplementary
if exist "%FileSupplementary%" goto ProcessFiles
echo ERROR: File "%FileSupplementary%" not found.
: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
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:" %%J"
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 &
and the conditional command operator ||
.
Read the Microsoft documentation about Using command redirection operators for an explanation of >nul
.