tipi
August 26, 2008, 4:19pm
1
Hi,
In a file FILE, the following lines appear :
WORD 8 8 8
ANOTHERWORD blabla
...
Directly in the prompt, if I type
$sed '/WORD/s/8/10/g' FILE
it replace the 8's by 10's in file :
$cat FILE
WORD 10 10 10
ANOTHERWORD blabla
...
This is exactly what I want to do. Then, if instead in the prompt, I define
$before=8
$increment=2
and if I type
$sed '/WORD/s/'$before'/'$[$before + $increment]'/g' FILE
It also work perfectly (8's replaced by 10's).
Now, if instead in a script, the following line appear in a while loop
max=12
increment=2
before=10
while [ $before -le $max ]
do
sed -i_temp '/WORD/s/'$before'/'$[$before + $increment]'/g' FILE
before=$[$before + $increment]
done
And if I run the script, it doesn't work! It replace "8 8 8" by "10" instead of "10 10 10"
What is my problem?
Thanks a lot for your help,
Tipi
tipi
August 26, 2008, 4:25pm
2
I forgot to say that max, before and increment are in fact arrays. So, when I call, $max, $increment, or $before, I call the element [0] of these arrays.
Maybe this can be the source of the problem? Instead of
max=12
increment=2
before=10
in the last post, you should read
max=( 12 12 12 )
increment=( 2 2 2 )
before=( 10 10 10 )
everything else stay the same.
This doesn't work for me :
If using variables in sed then use double quotes. Try this :
aigles
August 26, 2008, 4:31pm
4
Replace single quotes by double quotes in your sed command (variables will be expanded) and use (( )) for calculations
max=12
increment=2
before=10
while [ $before -le $max ]
do
sed -i_temp "/WORD/s/$before/$((before + increment))/g" FILE
(( before = $before + $increment ))
done
Jean-Pierre.
tipi
August 26, 2008, 4:37pm
5
My script is really huge so, right after the sed line, I added
echo "before+increment=$[$before+$increment]" >> REPORTFILE
echo "Wrote : `grep WORD FILE`" >> REPORTFILE
When I look in the REPORTFILE file, I can read :
before+increment=12
Wrote : WORD 12
So
WORD 10 10 10
really became
WORD 12
This is the same with single quote like in my fisrt post, or with double quoted sed :
sed -i_temp "/WORD/s/$before/$[$before + $increment]/g" FILE
Thanks a lot for your help,
Tipi
tipi
August 26, 2008, 4:42pm
6
aigles:
Replace single quotes by double quotes in your sed command (variables will be expanded) and use (( )) for calculations
max=12
increment=2
before=10
while [ $before -le $max ]
do
sed -i_temp "/WORD/s/'$before'/'$((before + increment))"/g' FILE
(( before = $before + $increment ))
done
Jean-Pierre.
Are the last " and ' to be interchanged? Because I have an open quote error...
Run ur script using sh -x and check what is the sed command that is getting executed.
tipi
August 26, 2008, 5:03pm
8
I run in bash on Mac OSX...
When I type /bin/bash -x myscript on the prompt, I find about sed (exactly) :
sed -i_temp /WORD/s/10/12/g FILE
is this ok?
tipi
August 26, 2008, 5:20pm
9
Ok, I found the problem. It had nothing to do with sed... sorry for that :o
That looks good to me.
Are you trying to replace WORD 8 8 8 with WORD 10 10 10 ??? Then it check the values of the variables, as in the above u r trying to replace all 10 with 12 after WORD on all the lines.