【问题标题】:Does not work "search and replace" in multiple files with html tags不能在带有 html 标签的多个文件中“搜索和替换”
【发布时间】:2017-11-12 13:27:02
【问题描述】:

我有带有标签的 html 文件,其中有变量、变量的内容和该变量的操作。

<tr>
    <td>click</td>
    <td>new</td>
    <td>Type_Account</td>
</tr>

Type_Account - 变量;
新 - 变量的内容;
点击 - 变量的动作。

我需要在脚本中确定变量在哪里(我可以找到它,因为下一个标签是&lt;/tr&gt;)。并在多个文件中搜索和替换此变量的内容。我需要对几个变量(在 html 文件中)进行处理,而不是一个。

我的脚本:

#!/bin/bash

cd ~/dir

line1="Type_Account"
IFS=","
array=( $line1 )

line2="pro"
IFS=","
array2=( $line2 )

for ((i=0;i<"${#array[@]}";++i)); do
# get </tr> tag. if we have <tr>, this is a variable
w=`grep -A 1 "<td>${array[$i]}</td>" *.html | grep -v "${array[$i]}"`
# get content of variable
w2=`grep -B 1 "<td>${array[$i]}</td>" *.html | grep -v "${array[$i]}"`
# get number of line, of content of variable
number=`grep -n -B 1 "<td>${array[$i]}</td>" *.html | grep -v "${array[$i]}" | cut -d "-" -f 1`

if [[ "$w" == *"</tr>"* ]]; then
        echo "ok"
        echo $w2
        sed -i -e "$number s/\(<td>\).*\(<\/td>\)/<td>${array2[$i]}<\/td>/g" *.html
else
        echo "not ok"
fi
done

如果我想为一个文件index.html 执行此操作,它可以工作。如果我尝试使用多个文件 *.html 它不起作用。此脚本的工作示例。

ndex.html
ndex2.html s/(<td>).*(</td>)/<td>pro</td>/g
<tr>
ndex.html
ndex2.html s/(<td>).*(</td>)/<td>pro</td>/g
    <td>click</td>
ndex.html
ndex2.html s/(<td>).*(</td>)/<td>pro</td>/g
    <td>new</td>
ndex.html
ndex2.html s/(<td>).*(</td>)/<td>pro</td>/g
    <td>Type_Account</td>
ndex.html
ndex2.html s/(<td>).*(</td>)/<td>pro</td>/g
</tr>

当我尝试为一个文件执行此操作时,我得到了很好的结果:

<tr>
    <td>click</td>
    <td>pro</td>
    <td>Type_Account</td>
</tr>

如何在我的脚本中处理多个文件?可能我应该在列表中工作吗? 我尝试对这些文件使用循环:

dir=`ls ~/dir/ | egrep '\.html'`
IFS=","
array3=( $dir )

for ((i=0;i<"${#array[@]}";++i)); do
w=`grep -A 1 "<td>${array[$i]}</td>" ${array3[$i]} | grep -v "${array[$i]}"`
w2=`grep -B 1 "<td>${array[$i]}</td>" ${array3[$i]} | grep -v "${array[$i]}"`
number=`grep -n -B 1 "<td>${array[$i]}</td>" ${array3[$i]} | grep -v "${array[$i]}" | cut -d "-" -f 1`

if [[ "$w" == *"</tr>"* ]]; then
        echo "ok"
        echo $w2
        sed -i -e "$number s/\(<td>\).*\(<\/td>\)/<td>${array2[$i]}<\/td>/g" ${array3[$i]}
else
        echo "not ok"
fi
done

但在调试中:

+ array3=($dir)
+ echo 'index.html
index2.html'
index.html
index2.html
+ (( i=0 ))
+ (( i<1 ))
++ grep -A 1 '<td>Type_Account</td>' 'index.html
index2.html'
++ grep -v Type_Account
grep: index.html
index2.html: No such file or directory
+ wwe=
++ grep -B 1 '<td>Type_Account</td>' 'index.html
index2.html'
++ grep -v Type_Account
grep: index.html
index2.html: No such file or directory
+ wwe2=
++ grep -n -B 1 '<td>Type_Account</td>' 'index.html
index2.html'
++ grep -v Type_Account
++ cut -d - -f 1
grep: index.html
index2.html: No such file or directory
+ number=
+ echo

+ echo

+ echo

+ [[ '' == *\<\/\t\r\>* ]]
+ echo 'not ok'
not ok
+ (( ++i ))
+ (( i<1 ))

它尝试为每个文件制作grep,它尝试制作grep index.html index2.html,当然 id 不起作用。请帮我。谢谢。

【问题讨论】:

    标签: bash for-loop if-statement search replace


    【解决方案1】:

    代替

    dir=`ls ~/dir/ | egrep '\.html'`
    IFS=","
    array3=( $dir )
    

    试试这个:

    array3=( ~/dir/*.html )
    

    【讨论】:

    • 对不起,我有同样的结果。
    • 你摆脱IFS=","了吗?这就是吸引你的原因。
    • 我当然做到了。在调试++ grep -A 1 '&lt;td&gt;Type_Account&lt;/td&gt;' ++ grep -v Type_Account 中,仅此而已。他不想进行下一步并逐个文件地在循环文件中进行grep。我只需要在几个文件中做一个操作。
    • 很抱歉 - 尝试一次对所有文件执行 grep?
    • 它尝试不做任何文件。在空旷的地方
    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2017-05-23
    • 2012-10-17
    相关资源
    最近更新 更多