1.

Solve : search and replace?

Answer»

Hi

I have two files

file 1 contains

VALUE_1
VALUE_2
VALUE_3

file 2 contains

parameter 1 has value &1
parameter 2 has value &2
parameter 3 has value &3

I WOULD like to replace the &1, &2 and &3 which the values of file one so &1 --> VALUE_1 etc. So when we are finished
files 2 contains the following

parameter 1 has value VALUE_1
parameter 2 has value VALUE_2
parameter 3 has value VALUE_3

How do I do this in a cmd file......

The following code takes every line from file1.txt and every line from file2.txt
so you have it in %%A and %%B and writes it in file3.txt.
This works if you will take the lines completely. If you want to filter it out it is a bit different.

snip---

for /f "tokens=*" %%A in (file1.txt) do for /f "tokens=*" %%B in (file2.txt) do ECHO %%B %%A >>file3.txt

-----snap


If you have exactly the same count of colums and want to have it lieke:

Parameter 1 has Value_1
...

snip----
for /f "tokens=*" %%A in (file1.txt) do for /f "tokens=1-4 delims= " %%B in (file2.txt) do echo %%B %%C %%D %%A
---------snap


hope it helps
uli
That's not quite what I want. In your we I concat just two files, thats not what i want. In file2 I have params which must be replaced by the values mentioned in file1.

VALUE_1 IN 10
VALUE_2 IN 20
VALUE_3 IN 30

file 2 contains

parameter 1 has value_1
parameter 2 has value_2
parameter 3 has value_3

So value_1,2 and 3 must be replace by 10,20 and 30 so the file should look like this

parameter 1 has value 10
parameter 2 has value 20
parameter 3 has value 30

But anyway thanx for the reponse!!

Pjotter
Like this?

Code: [Select]for /f "tokens=1,2,*" %%a in (file1.txt) do set %%a=%%c
for /f "tokens=*" %%a in (file2.txt) do (
set line=%%a
for /f "tokens=1,* delims==" %%a in ('set value_') do (
call set line=%%line:%%a=%%b%%
)
call echo.%%line%%
)>file3.txt
move /y file3.txt file2.txt >NUL
Should also work CORRECTLY when:
- The values in file1 contain text with space
- A line in file2 has multiple values to replace

Same is much easier when the syntax for a variable to be replaced in file2 is like %value_1%, i.e.:
parameter 1 has %value_1%
parameter 2 has %value_2%
parameter 3 has %value_3%

In which case the script would simplify to:
Code: [Select]for /f "tokens=1,2,*" %%a in (file1.txt) do set %%a=%%c
(for /f "tokens=*" %%a in (file2.txt) do call echo.%%a)>file3.txt
move /y file3.txt file2.txt >NUL
DOS IT HELP? MANY THANKS!!!!

For 99% procent it covers what I want, the 1% is the next issue file2 does indeed contain the params as described in file1 BUT the PREFIX in this case is an & so file2 looks like

parameter 1 has &value_1 <---- watch for the extra & in each row
parameter 2 has &value_2
parameter 3 has &value_3

instead of

parameter 1 has value_1
parameter 2 has value_2
parameter 3 has value_3


so the line for /f "tokens=1,* delims==" %%a in ('set value_') do ( should be something like for /f "tokens=1,* delims==" %%a in ('set &value_') do (

BUT & is some kind of preserved word. How do I overcome this?

DOSItHelp DOES indeed help!!!!!
pjotter



:-? :-? :-?
when chaning VALUE_1 to P_ the every lines ends with 3 c's

When changing P_PARAM1 IN 10 to V_PARAM1 IN 10 in file1 the output is

parameter 1 10c c c

strange, is P_ somekind reserved word??

just an alternative. Not in batch but in Python
file1.txt contains
VALUE_1
VALUE_2
VALUE_3

file2.txt contains
parameter 1 has value &1
parameter 2 has value &2
parameter 3 has value &3


Code: [Select]all = open("file1.txt").read().split() ##"all" contains ['VALUE_1', 'VALUE_2', 'VALUE_3']
count = 1
store = {}
for items in all:
## store will contain {'&1': 'VALUE_1', '&2': 'VALUE_2', '&3': 'VALUE_3'}
## a mapping of your &1 with VALUE_1 and so on
store['&' + str(count)] = items
count = count + 1

p = open("file2.txt")
for lines in p:
lines = lines.strip() #strip off newlines
print lines[:-2] + store[ lines[-2:] ]

p.close()

Output:
Code: [Select]parameter 1 has value VALUE_1
parameter 2 has value VALUE_2
parameter 3 has value VALUE_3




Or in batch:

Code: [Select]for /f "tokens=1,2,*" %%a in (file1.txt) do set %%a=%%c
for /f "tokens=*" %%a in (file2.txt) do (
set line=%%a
for /f "tokens=*" %%a in ('set value_') do call set line=%%line:^&%%a%%
call echo.%%line%%
)>file3.txt
move /y file3.txt file2.txt >NUL[highlight]; )[/highlight]



Discussion

No Comment Found