0

I have 3 files:

  • the first file is a list of IDs
  • the second file is the source strings
  • the third file is composed by target strings.

Eg

File 1
3952276-0-1
3952276-0-2
3952276-0-3
3952276-0-4
3952276-0-5
3952276-0-6
3952276-0-7
3952276-0-8
3952276-0-9
3952276-0-10

File 2
source-string1
source-string2
source-string3
source-string4
source-string5
source-string6
source-string7
source-string8
source-string9
source-string10

File 3
target-string1
target-string2
target-string3
target-string4
target-string5
target-string6
target-string7
target-string8
target-string9
target-string10

I want a result file in csv like with the exception if the the target-string is the same of source string, DO NOT COPY in ""... leave the "" of target-string empty if it's the same of source

"3952276-0-1","source-string1","target-string1"
"3952276-0-2","source-string2","target-string2"
"3952276-0-3","source-string3","target-string3"

How do i that? Thx in advance

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49

2 Answers2

3
@echo off
setlocal EnableDelayedExpansion

rem File 1 is read with the FOR command
rem File 2 and File 3 are read via standard handles 3 and 4, respectively

3< file2.txt 4< file3.txt (for /F "delims=" %%a in (file1.txt) do (
   set /P "source=" <&3
   set /P "target=" <&4
   if "!target!" neq "!source!" (
      echo "%%a","!source!","!target!"
   ) else (
      echo "%%a","!source!",""
   )
)) > output.txt

For further details on the method used to read several files, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

This should work:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET file1=1.txt
SET file2=2.txt
SET file3=3.txt
SET targetfile=target.txt
TYPE NUL>%targetfile%
SET i=0
FOR /F %%l IN (%file1%) DO (
    SET x!i!=%%l
    SET /a i=!i!+1
)
SET i=0
FOR /F %%l IN (%file2%) DO (
    SET y!i!=%%l
    SET /a i=!i!+1
)
SET i=0
FOR /F %%l IN (%file3%) DO (
    SET z!i!=%%l
    SET /a i=!i!+1
)
SET /a i=!i!-1
FOR /L %%n IN (0,1,!i!) DO (
    ECHO "!x%%n!","!y%%n!","!z%%n!">>%targetfile%
)

First we load each line of file1 into the variables x1, x2, x3,..., xi. Then we do the same with the 2nd and 3rd file and store them in y1,...,y1 and z1,...,zi. Here it's required that all three files have the same number of lines.

Finally we write "xi","yi","zi" into the target file.

MichaelS
  • 5,941
  • 6
  • 31
  • 46