A good mate on a forum had helped me with creating this script but everything Ive thrown at it outputs wrong and I do not know why or what is the issue. I ran all my script in ubuntu's terminal if questioned as bash test.sh.
My goal is to increment several .xml file's process></process>
tags, but some files could have 1 to 100 tags.
Example:
- jfksaJDFH
- <process>value=""</process>
- <process>value=""</process>
- <process>value=""</process>
- <process>value=""</process>
- jdhkjasdh
- <process>value=""</process>
- <process>value=""</process>
- <process>value=""</process>
- <process>value=""</process>
After script:
- jfksaJDFH
- <process>value="1"</process>
- <process>value="2"</process>
- <process>value="3"</process>
- <process>value="4"</process>
- jdhkjasdh
- <process>value="5"</process>
- <process>value="6"</process>
- <process>value="7"</process>
- <process>value="8"</process>
Script:
#!/bin/bash
dir="/mnt/Desktop/test/"
while IFS= read -r -d '' file
do
i=1
while IFS= read -r -u 3 line
do
if [[ $line = '<process></process>' ]]; then
echo "<process>value=\"$((i++))\"</process>"
else
echo "$line"
fi
done 3< "$file" > "$file.xml"
done < <(find $dir -type f -name \*.xml -print0)
when script above is ran removes last <process>value=""</process>
modify script to this:
while IFS= read -r -d '' file
do
i=1
while IFS= read -r -u 3 line
do
if [[ $line = '<process></process>' ]]; then
echo "$line"
else
echo "<process>value=\"$((i++))\"</process>"
fi
done 3< "$file" > "$file.xml"
done < <(find $dir -type f -name \*.xml -print0)
Output of file is this:
<process>value="1"</process>
<process>value="2"</process>
<process>value="3"</process>
<process>value="4"</process>
<process>value="5"</process>
<process>value="6"</process>
<process>value="7"</process>
<process>value="8"</process>
<process>value="9"</process>
<process>value="10"</process>
<process>value="11"</process>
<process>value="12"</process>
<process>value="13"</process>
<process>value="14"</process>
<process>value="15"</process>
<process>value="16"</process>
<process>value="17"</process>
<process>value="18"</process>
<process>value="19"</process>
<process>value="20"</process>
<process>value="21"</process>
<process>value="22"</process>
<process>value="23"</process>
which in other words increments but removes all other content in the page.