Solved: Change values from .txt file

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

fscldhppr

Thread Starter
Joined
Mar 29, 2010
Messages
30
If one variable in file1.txt matches identical variable in file2.txt , change that variable from file1.txt to the "=" value from text2.txt, and save as corrected value(only)to file3.txt (keeping the variables in order from file1.txt)

Example:

file1.txt contains...

RED52
GREEN66
YELLOW23
purple45

file2.txt contains...

purple45 = 555555
GREEN66 = 333333
RED52 = 888888
YELLOW23 = 222222

output file(file3.txt)

888888
333333
222222
555555

Thanks guys.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Code:
@echo off
FOR /F "Delims=" %%G IN (file1.txt) DO (
	FOR /F "Tokens=1,2 Delims==" %%H IN ('findstr /B /I "%%G" ^<file2.txt') DO >>file3.txt echo %%I
)
 

fscldhppr

Thread Starter
Joined
Mar 29, 2010
Messages
30
Thanks Squashman, sorry for the late reply.

I created a folder with file1.txt & file2.txt containing the information listed from the original post, along with the .bat file to try it out. Not too familiar with batch/script language...was just wondering if your .bat required other parameters to be entered, as-is I'm getting no output...pardon my lack of understanding.

BTW, is "The Outcaste" still around? He did some work for a MS Flight Simulator/traffic data project I had a few years back. I am somewhat getting back into the hobby after medical/other hiatus, and(when time permits)still use the batch file he created for sorting extracted PDF timetable data.
 

foxidrive

Banned
Joined
Oct 20, 2012
Messages
793
I created a folder with file1.txt & file2.txt containing the information listed from the original post, along with the .bat file to try it out. Not too familiar with batch/script language...was just wondering if your .bat required other parameters to be entered, as-is I'm getting no output...pardon my lack of understanding.
FWIW the code works here with your data files.
 

fscldhppr

Thread Starter
Joined
Mar 29, 2010
Messages
30
OK, thanks foxidrive...I just copied Squashman's text into notepad and saved as a .bat. Clicked on the .bat and the cmd box just stays open(no file3.txt output), tried dropping file1.txt into the .bat, no output.

I figured I must not be doing something right, or simply not using it in the right context/format for which it is meant...sorry, complete novice at this.
 

foxidrive

Banned
Joined
Oct 20, 2012
Messages
793
The problem might very well be that file2.txt does not have a trailing carriage return/line feed
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
List the text files in the directory.
Code:
C:\BatchFiles\FINDSTR>dir /b File*.txt
file1.txt
file2.txt
Listing the contents of file1.txt
Code:
C:\BatchFiles\FINDSTR>type file1.txt
RED52
GREEN66
YELLOW23
purple45
Listing the contents of file2.txt
Code:
C:\BatchFiles\FINDSTR>type file2.txt
purple45 = 555555
GREEN66 = 333333
RED52 = 888888
YELLOW23 = 222222
Listing the contents of the batch file
Code:
C:\BatchFiles\FINDSTR>type match.bat
@echo off
FOR /F "Delims=" %%G IN (file1.txt) DO (
        FOR /F "Tokens=1,2 Delims==" %%H IN ('findstr /B /I "%%G" ^<file2.txt') DO >>file3.txt echo %%I
)
running the batch file
Code:
C:\BatchFiles\FINDSTR>match.bat
Listing the contents of the output file named file3.txt
Code:
C:\BatchFiles\FINDSTR>type file3.txt
 888888
 333333
 222222
 555555
 

foxidrive

Banned
Joined
Oct 20, 2012
Messages
793
I changed my post above Squashman - it's probably the findstr bug with no CR/LF on file2.txt
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
This seems to fix that problem
Code:
@echo off
FOR /F "Delims=" %%G IN (file1.txt) DO (
	FOR /F "Tokens=1,2 Delims==" %%H IN ('type file2.txt ^|findstr /B /I "%%G"') DO >>file3.txt echo %%I
)
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Wow. You have been gone a long time. Two threads in 4 years.
 

fscldhppr

Thread Starter
Joined
Mar 29, 2010
Messages
30
...works great now, thank you!

Yeah, I've had to deal with some medical/other issues for myself and my family for quite some time, that took me away from a lot of things for a while.

The Outcaste had a lot of patience dealing with someone like me trying to explain what exactly I was trying to do with my Flight Sim project...someone also with little to no experience working with batch files/scripts/etc.... but he pretty much nailed it right off the bat(no pun), and did it so fast I couldn't believe it.

Probably wasn't much of a challenge compared to some of the things you guys do here, but I sure appreciated it.

Both of you made this novice feel comfortable asking "stupid questions" on that "txt/data" thread.

..thanks again Squashman...and thank you foxidrive.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top