我需要更改HTML文件的价格,它会搜索并将它们存储在数组中,但我必须更改并保存/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
Run Code Online (Sandbox Code Playgroud)
输出"cat /home/.../nuevo-focus.html|grep -oiE'([$] [0-9.] {1,7})'| tr'\n'''`)"是...
$86.880 $0 $912 $908 $902 $897 $882 $812 $25.725 $715
Run Code Online (Sandbox Code Playgroud)
在bash变量中$0通过$9引用正在运行的脚本的相应命令行参数.在线:
price2=( $90.880 $0 $920 $925 $930 $910 $800 $712 $27.220 $962 )
Run Code Online (Sandbox Code Playgroud)
它们将扩展为空字符串或您为脚本提供的命令行参数.
尝试这样做:
price2=( '$90.880' '$0' '$920' '$925' '$930' '$910' '$800' '$712' '$27.220' '$962' )
Run Code Online (Sandbox Code Playgroud)
编辑第二部分问题
如果您尝试使用该sed行替换文件中的价格,覆盖旧的价格,那么您应该这样做:
sed -i s/$item/$x/g /home/delkav/info-sitioweb/html/productos/autos/nuevo-focus.html
Run Code Online (Sandbox Code Playgroud)
这将在place(-i)中执行替换,修改输入文件.
编辑问题的第三部分
我刚刚意识到你的嵌套循环并没有多大意义.我假设您想要做的是用price相应的价格替换每个价格price2
如果是这种情况,那么你应该使用一个循环,循环遍历数组的索引:
for i in ${!price[*]}
do
sed -i "s/${price[$i]}/${price2[$i]}/g" /home/delkav/info-sitioweb/html/productos/autos/nuevo-focus.html
done
Run Code Online (Sandbox Code Playgroud)
我现在无法测试,但我认为它应该完成你想要的.
要解释一下:
${!price[*]}为您提供数组的所有索引(例如0 1 2 3 4 ...)
对于每个索引,我们然后用新的索引替换相应的旧价格.您没有像现在这样需要嵌套循环.当你执行它时,你基本上做的是这样的:
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...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |