0

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
Ivan I.
  • 3
  • 2
  • Thank you for your latest [Edit]. This needs more clarification however. Is this always going to be two strings, the first always a number in integer form and the second a single string colour? So you cannot want to match both in any order, `red 12`, and you cannot have `red & pink`, `yellow and green`, `blue green`, `8 dark blue`, `15 light green`. – Compo May 29 '23 at 18:07

2 Answers2

0
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 "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

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"

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hi, thanks for answer.. but after i tested, I get outfile same as filename1. In my case Final.txt is same as 1.txt – Ivan I. May 29 '23 at 13:39
0

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143