0

How can I do this in Python 2.7:

I have one file named e.g. parameter-values.txt which contains 10 or more parameters with neccessary values like this:

param1=hello,dd
param2=test
param3=4g

etc..

and also I have a lot of files where is this variables defined (but not all params in every file).

File1:

#!/bin/bash
param1="&*"
param3="&*"

another bash commands

So, my question is: How can I recursively in loop find files which contains this parameters/variables and substitute parameters in these files with my own in the parameters file.

Thank for every relevant answer.

turbulencetoo
  • 3,447
  • 1
  • 27
  • 50
tester125
  • 21
  • 7
  • Do you want to replace `param1` in File1 with another value? (Which one? Should it be `hello`, `dd` or `hello,dd`?) – zezollo May 05 '16 at 13:07
  • Must `parameter-values.txt` contain the values exactly the way you describe? Can't it be a simple "ini" file? – zezollo May 05 '16 at 13:08
  • Its can be everything but I never used ini, and I want to change param1 in all files no only in file1, In all files which contains it, and calues is "hello,dd" or whatever. – tester125 May 05 '16 at 13:11
  • OK, and where's your code? You won't get much help if you do not provide any. Now, as a hint, you could put your parameters in an ini file (check https://docs.python.org/2/library/configparser.html and http://stackoverflow.com/questions/8884188/how-to-read-and-write-ini-file-with-python). In order to replace the `"&*"` values, I'm not sure I would go for a python script, but maybe a shell script. – zezollo May 05 '16 at 13:21
  • I have not got any script yet, Now I'm finding solution for my problem. And thanks for links I read it, And I have only a lot of bash scripts with 1-5 variables and I want to change it from my one parameters config file, recursively, when I do change in one parameter in "param's file" I want to do change in value by script in all bash scripts As i wrote. And that ini looks good, so I try it, – tester125 May 05 '16 at 13:27
  • why recursively? why not just a loop? – kmaork May 05 '16 at 13:32

1 Answers1

0

Solution:

item_arr=(test= param2= paramX=)
new_param_arr=(test=\"hello\" param2=\"WWW\" paramX=\"XXX\")

var=0
for oldstring in "${item_arr[@]}"
do
grep -rl $oldstring | xargs sed -i "s/.*$oldstring.*/${new_param_arr[$var]}/g"
#echo -e "$oldstring\e[92m"
echo -e "${new_param_arr[$var]}\e[92m"
var=$((var+1))
done

I think this is best and simplest solution hwhich works properly

tester125
  • 21
  • 7