1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Change values from .txt file

Discussion in 'DOS/Other' started by fscldhppr, Jan 27, 2015.

Thread Status:
Not open for further replies.
Advertisement
  1. fscldhppr

    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.
     
  2. Squashman

    Squashman 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
    )
     
  3. fscldhppr

    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.
     
  4. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    FWIW the code works here with your data files.
     
  5. fscldhppr

    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.
     
  6. foxidrive

    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
     
  7. Squashman

    Squashman 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
     
  8. foxidrive

    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
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Unfortunately no. I miss him dearly. I wish I knew what happened to him. I hope to catch up with him some day. I hope he is ok.
     
  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Yikes! That is nasty. I just tested and the batch file just hangs!!!!
     
  11. Squashman

    Squashman 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
    )
     
  12. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Wow. You have been gone a long time. Two threads in 4 years.
     
  13. fscldhppr

    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.
     
  14. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1141988

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice