Capturing tokens only once from two .txt files?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Capturing tokens only once from two .txt files?

#1 Post by Cat » 14 Feb 2012 22:24

I'm writing a code that will combine lines from two text files. The first text file contains:

Code: Select all

A
B
C
D

The second one contains:

Code: Select all

1
2
3
4

I want the output to be:

Code: Select all

A 1
B 2
C 3
D 4


Any ideas how to make this work? My best attempt results with:

Code: Select all

A 1
A 2
A 3
A 4
B 1
B 2
B 3
B 4
C 1
...


And so on like that. This uses two seperate FOR commands with synchronized tokens.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Capturing tokens only once from two .txt files?

#2 Post by Aacini » 15 Feb 2012 01:52

Code: Select all

@echo off
setlocal EnableDelayedExpansion
< file2.txt ( for /F "delims=" %%a in (file1.txt) do (
   set file2Line=
   set /P file2Line=
   echo %%a !file2Line!
   )
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Capturing tokens only once from two .txt files?

#3 Post by foxidrive » 15 Feb 2012 02:45

That's neat Aacini

This modification does away with the need for delayed expansion.

Code: Select all

@echo off
setlocal
< file2.txt ( for /F "delims=" %%a in (file1.txt) do (
   set file2Line=
   set /P file2Line=
   for /f "tokens=1* delims==" %%b in ('set file2Line') do (
   echo %%a %%c
   )
   )
   )
pause


Poison characters and all.

Code: Select all

A 1 & a !b!
B 2 ^c
C 3 %d
D 4 |e

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Capturing tokens only once from two .txt files?

#4 Post by jeb » 15 Feb 2012 03:01

But now file contents with equal signs are removed

file2.txt wrote:===1
=2
===3


As the "delims==" option split at the delim "=" and removes all subsequential delims

jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Capturing tokens only once from two .txt files?

#5 Post by foxidrive » 15 Feb 2012 03:44

jeb wrote:But now file contents with equal signs are removed

file2.txt wrote:===1
=2
===3


As the "delims==" option split at the delim "=" and removes all subsequential delims

jeb


Only leading equals signs.

Code: Select all

A 1 & a !b! = =
B 2 ^c ===
C 3 %d ==
D 4 |e1 =

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Capturing tokens only once from two .txt files?

#6 Post by jeb » 15 Feb 2012 08:43

Or you could use the delayed toggling technic to be always safe :)

Code: Select all

@echo off
setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
      echo(!file1Line! !file2Line!
   )
)


jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Capturing tokens only once from two .txt files?

#7 Post by foxidrive » 15 Feb 2012 10:05

Yep! That's a winner!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Capturing tokens only once from two .txt files?

#8 Post by Ed Dyreen » 15 Feb 2012 11:12

jeb wrote:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
      echo(!file1Line! !file2Line!
      endlocal
   )
)
Jeb, you are mean, you should endlocal :twisted: , besides you are eating memory AND slowing down performance :lol:

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Capturing tokens only once from two .txt files?

#9 Post by jeb » 15 Feb 2012 14:16

You are right.

I forgot one of the key elements of the toggling technic.
Without the endlocal it doesn't toggle and it would fail :!:

I am so lazy :oops:

Post Reply