Replacing string

Hi there,

I'd like to replace STRING_ZERO in FILE_ZERO.txt with the value of VALUEi-th by using something like that:

VALUE1=1000
VALUE2=2000
VALUE3=3000
for((i=1;i<=3;i++));
do
sed "s/STRING_ZERO/$VALUE'$i'/" FILE_ZERO.txt >> FILE_NEW.txt;
done

but it doesn't work...
Any help will be appreciated!!!:smiley:

Giordano Bruno

VALUES="1000 2000 3000"
for VAL in "${VALUES}"; do
        sed 's/STRING_ZERO/'${VAL}'/' FILE_ZERO.txt >> FILE_NEW.txt
done

exit 0

If still want to have the name of a variable being built partially by another variable, you should build it using "eval".

Thank you!!!
Sorry for my silly question... but if I want to replace STRING_ZERO and obtain at every step a FILE_NEWith.txt???

What is your expected input and output - not how to do something?

No silly question ^^

Do you want 3 different output files then? Just have in the filename the ${VAR} for example.
Currently you have it that everything is appended to a single outfile.

Please give a look at my first message, what I meant was how could obtain a FILE_NEWi-th for every single VALUEi-th and replace STRING_ZERO in FILE_ZERO with this value???

Me again :smiley: ...

the problem is that I can't obtain the single value, but the three values together, for example if I try this:

VALUES="1000 2000 3000"
for VAL in "${VALUES}"; do
echo "Ecco il valore:'$VAL'"
done
exit 0

I get:

'1000 2000 3000'

sed "s/STRING_ZERO/$(eval echo \$VALUE$i)/g" FILE_ZERO.txt >> FILE_NEW.txt

Thanks a lot!!!
Now it works!:b:

I have another question (the last one for today) I was looking for conditional compilation in bash script, something like #if... #endif in C language, but I couldn't find any information ...
Any suggestions???

The problem in your previous post was this: the shell has to decide what is an argument and what are several arguments. Usually the space character (or any other whitespace for that matter) is used for that:

mv filea fileb

is interpreted as to take a file "filea" and rename (mv) it to "fileb". But lets suppose you have a file with whitespace in its name, how would you make the shell aware that

mv partone parttwo new name

should mean to take a file called partone parttwo and rename it to new name? How can the shell decide that you are not talking about 4 filenames, each separated by a blank or a file named partone parttwo new which should be renamed to name, etc.?

This is done by enclosing strings into double quotes. This groups the strings and make the shell aware where the boundaries are, even when the expression would be ambigous otherwise:

mv "partone parttwo" "new name"

This version removes all ambiguity.

In light of this lets consider your first line:

VALUES="1000 2000 3000"
for VAL in "${VALUES}"; do

The first thing the shell does is to replace the "${....}" with the content of the variable, so you commandline reads, after this:

VALUES="1000 2000 3000"
for VAL in "1000 2000 3000"; do

And now you should see the problem: you present a SINGLE element to "for" and therefore get a single pass of your loop. Removing the double quotes would do the trick:

VALUES="1000 2000 3000"
for VAL in ${VALUES}; do

This of course would mean that your values should not contain any whitespace, because the shell would split them and interpret them as two distinct values. To overcome this you should use an array and a while-loop:

array[0]="1000"
array[1]="2000"
array[2]="3000"
array[3]="4000"
array[4]="5000"
array[5]="ab cd"  # notice the whitespace
array[6]="6000"
iCnt=0

while [ $iCnt -lt ${#array[@]} ] ; do
     echo "array element number $iCnt is: \"${array[$iCnt]}\""
     (( iCnt += 1 ))
done

Regarding the "conditional compilation": there is no such thing, because bash is an interpreted language. You could write any garbage into the program code, as long as there is no attempt to execute it would never show:

if [ 1 = 0 ] ; then
     this is never going to be executed
     and it does not matter that this makes no sense at all as a
     bash-script
else
     echo "would you exchange the if- and else-branches"
     echo "you would get several errors"
fi

I hope this helps.

bakunin