samedi 10 juin 2017

Does not work "search and replace" in multiple files with html tags

i have html file with tags, where i have variable, content of variable, and action for this variable.

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

Type_Account - variable;
new - content of variable;
click - action of variable.

I need in script, to determine, where is variable (i can found it, because next tag is </tr>). And search and replace content of this variable in multiple files. And i need to do it with for several variables (in html file), not for one.

My script:

#!/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

If i am trying to do it for one file index.html, it works. If i am trying to use multiple files *.html it does not work. Example of work of this script.

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>

When i am trying to do it for one file, i have good result:

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

How can i work with multiply files in my script ? May be i should work in a list ? I tried to use loop for these files:

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

But in a debug:

+ 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 ))

It tries to make grep not for each file, it tries to make grep index.html index2.html and of course id does not work. Please help me. Thanks.

Aucun commentaire:

Enregistrer un commentaire