1

I need change price the HTML file, which search and store them in array but I have to change and save /nuevo-focus.html

price=( `cat /home/delkav/info-sitioweb/html/productos/autos/nuevo-focus.html | grep -oiE '([$][0-9.]{1,7})'|tr '\n' ' '` )

 price2=( $90.880 $0 $920 $925 $930 $910 $800 $712 $27.220 $962 )
 sub (){
 for item in "${price[@]}"; do
    for x in ${price2[@]}; do
      sed s/$item/$x/g > /home/delkav/info-sitioweb/html/productos/autos/nuevo-focus.html
     done
 done
 }

 sub

Output the "cat /home/.../nuevo-focus.html|grep -oiE '([$][0-9.]{1,7})'|tr '\n' ' '` )" is...

$86.880 $0 $912 $908 $902 $897 $882 $812 $25.725 $715

1 Answer 1

5

In bash the variables $0 through $9 refer to the respective command line arguments of the script being run. In the line:

price2=( $90.880 $0 $920 $925 $930 $910 $800 $712 $27.220 $962 )

They will be expanded to either empty strings or the command line arguments that you gave the script.

Try doing this instead:

price2=( '$90.880' '$0' '$920' '$925' '$930' '$910' '$800' '$712' '$27.220' '$962' )

EDIT for part two of question

If what you are trying to do with the sed line is replace the prices in the file, overwriting the old ones, then you should do this:

sed -i  s/$item/$x/g /home/delkav/info-sitioweb/html/productos/autos/nuevo-focus.html

This will perform the substitution in place (-i), modifying the input file.


EDIT for part three of the question

I just realized that your nested loop does not really make sense. I am assuming that what you want to do is replace each price from price with the corresponding price in price2

If that is the case, then you should use a single loop, looping over the indices of the array:

for i in ${!price[*]}
do
    sed -i  "s/${price[$i]}/${price2[$i]}/g" /home/delkav/info-sitioweb/html/productos/autos/nuevo-focus.html
done

I'm not able to test that right now, but I think it should accomplish what you want.

To explain it a bit:

${!price[*]} gives you all of the indices of your array (e.g. 0 1 2 3 4 ...)

For each index we then replace the corresponding old price with the new one. There is no need for a nested loop as you have done. When you execute that, what you are Basically doing is this:

replace every occurence of "foo" with "bar"
# at this point, there are now no more occurences of "foo" in your file
# so all of the other replacements do nothing
replace every occurence of "foo" with "baz"
replace every occurence of "foo" with "spam"
replace every occurence of "foo" with "eggs"
replace every occurence of "foo" with "qux"
replace every occurence of "foo" with "whatever"
etc...
Sign up to request clarification or add additional context in comments.

6 Comments

ok, i understand but in sentence code sed 's/$intem/$x/g' not work, because? and output save in file html, how?
I've updated my answer to show how you can do what I think you are trying to do.
thanks, this work! but how change array price for array2?? my code not work wich two for.
I don't understand what you are asking, can you rephrase that somehow?
in for this happens, sed -i s/$715/$90.880, $0, $920/g, etc./g and inverted sed -i s/$86.880, $0, $920, etc/$90.880/g - in the end nothing change
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.