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: way to find replace integers from a list?

Discussion in 'Software Development' started by necrovamp, Apr 6, 2010.

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

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    ok, here is my situation, I have 2 lists, one is a list of numbers, and the other is a random document with lets say the word sale in it.

    I'm looking for a program or a way, batch file or w/e, command, or anything at all to do this.

    I need to replace every instance of the word sale in one document, with a number from the list


    for example
    If this is the list of numbers

    and this is the document

    the end result would need to be

    thats it



    (Also to the admins, if there is a better section this can be put in, please move it, this is the only one I could find that seemed appropriate.)
     
  2. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    ps, I took a tiny bit of basic programming, so I might be able to figure something out if that is the only way.
     
  3. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Is this a Word document or a text file?
     
  4. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    well they are text files, but I could easily change them to word documents if needed
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Code:
    @echo off
    SetLocal EnableDelayedExpansion
    set _filename=myfile.txt
    For /F "usebackq tokens=* delims=" %%I in ("%_filename%") do (
    set str=%%I
    set str=!str:sale,=45,!
    set str=!str:sale.=67.!
    echo.!str!>>"new_%_filename%"
    )
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Since you marked this solved I assumed this worked for you. But since these are text files you realize it would have been just as easy to open them in any editor and done a find and replace.
     
  7. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    I marked it solved because it is to complicated to be worth the effort.
    I have a list of 1,100 integers, and I needed to replace every word price (price appears in my document 1,100 times), with an integer from the list of 1,100.

    So, I was just looking for a simple way to do that, then opening find/replace, and doing it one by one.

    I guess I should have just said, the way you suggested would be more work than doing find/replace 1,100 times.




     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    If you could post a test file with your data and a test file with all your numbers we might be able to figure something out.
     
  9. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    here are 2 files

    the integer file is the data
    and the value file is where numbers from the integer file need to replace every instance of the word data.
     

    Attached Files:

  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Guess I am not understanding from those examples. Can you post a few lines of what you want the output to look like based on the two input files you just gave me.
     
  11. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    I don't see the point of the value.txt file at all. Every line is identical, so would be easier to just generate a file with that line from the list of numbers.
    And how did we go from 1,100 numbers to 11,461?
    We also went from replacing two occurrences of the word sale to one occurrence of the word Price.
    Then in the example, instead of replacing Price, you want to replace data instead, and not change Price
    Hard to follow when you keep changing what you want to do
     
  12. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    Well the actual use is very different, I'm just providing examples based on your requests.




     
  13. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    this would be the first 5 lines.

     
  14. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    How you do it will be very different depending on exactly what you want done.
    This will replace data in the values.txt file with the numbers.
    It takes about 4 1/2 minutes to run with those two files:
    Code:
    @Echo %dbg%Off
    SetLocal EnableDelayedExpansion
    Set _IFile=integers.txt
    Set _VFile=value.txt
    Set _OFile=NewValues.txt
    If Exist "%_OFile%" Del "%_OFile%"
    Set _Count=0
    Echo. Reading Values, please wait.
    For /F "Usebackq Tokens=* Delims=" %%I In ("%_IFile%") Do (
      Set _Int!_Count!=%%I
      Set /A _Count+=1
    )
    Echo. Substituting Values, please wait.
    Set _Count=0
    For /F "Usebackq Tokens=* Delims=" %%I In ("%_VFile%") Do (
    Call :_Insert !_Count! "%%I"
    Set /A _Count+=1
    )
    Goto :EOF
    ::::::::::::::::::::::::::::::::::::::::::::::::::::
    :_Insert
    Set _Tmp=!_Int%1!
    Set _Line=%~2
    >>"%_OFile%" Echo.%_Line:data=!_Tmp!%
    
    This creates the same file from the list of numbers but only takes 9 seconds.
    Code:
    @Echo %dbg%Off
    Echo.%time%
    SetLocal EnableDelayedExpansion
    Set _IFile=integers.txt
    Set _OFile=NewValues.txt
    If Exist "%_OFile%" Del "%_OFile%"
    Echo. Creating file, please wait.
    For /F "Usebackq Tokens=* Delims=" %%I In ("%_IFile%") Do Echo.Price is value=“%%I”>>"%_OFile%"
    Echo.%time%
    Only works if each line is identical except for the number you are inserting.

    If the word you are replacing appears more than once in any one line, you would have to use a different method, as the first method would replace all occurrences in each line with the same number.
    Example, if you had a line like this:
    Items have been repriced. The new price is $price
    and you want to replace price, you'd get this:
    Items have been re391409d. The new 391409 is $391409
    For this you'd have to replace $price with $<number>, or know that you are replacing the last 5 characters on each line, or something else to uniquely identify the text being replaced on each line.
    Hope this will give you some ideas of how to do what you want.
     
  15. necrovamp

    necrovamp Thread Starter

    Joined:
    Oct 13, 2007
    Messages:
    220
    thanks, that answered my question :)
     
  16. 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/915186

  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