Solved: way to find replace integers from a list?

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.

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

I had one for sale, and I had another for sale.
the end result would need to be

I had one for 45, and I had another for 67.
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.)
 

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.
 

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
 

Squashman

Retired 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%"
)
 

Squashman

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

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.




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.
 

Squashman

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

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.
 

Attachments

Squashman

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

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.




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
 

necrovamp

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

Price is value=“391409”
Price is value=“468474”
Price is value=“514814”
Price is value=“778470”
Price is value=“866468”
 
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.
 
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

Staff online

Top