EDIT: Since OP clarified question more so adding code as per that now.
Assuming following is the Input_file.
cat Input_file
aaaaaa
bbbbbib
<details>
<summary>
singh1
singh2
test1 ba bla bla
</summary>
<div>
whwiuwviweivbw
wivuibwuivweiweg
wkvbwjvbwjbvwbviwrbhb
wvhwrivbwvbwrvbw
</div>
</details>
bfifiefe
fjbfiuebfiewfhbew
jwnjwnjwevbw
Now run following code.
awk -v RS="^$" '
{
gsub(/<details>\n<summary>.*<\/summary>/,".\n</summary>")
gsub(/<\/summary>\n<div>.*<\/div>/,"[%collapsible]" ORS "====" ORS "</div>")
gsub(/<\/div>\n<\/details>/,"====")
}
1
' Input_file
Output will be as follows.
aaaaaa
bbbbbib
.
[%collapsible]
====
</div>
whwiuwviweivbw
wivuibwuivweiweg
wkvbwjvbwjbvwbviwrbhb
wvhwrivbwvbwrvbw
====
bfifiefe
fjbfiuebfiewfhbew
jwnjwnjwevbw
Could you please try following, I have tested this with gawk
and with one test Input_file and it worked successfully, would request you to check it with 1 Input_file once and once Happy with results try it out on *.html
files then.
First set current value variable as old_text
shell variable:
old_text="+++ <details><summary> +++
some description
+++ </summary><div> +++
this
is
going
to be
folded
+++ </div></details> +++"
Now set shell variable named new_text
with new text value which you want newly in Input_file(s).
new_text=".some description
[%collapsible]
====
this
is
going
to be
folded
===="
Now run following code on Input_file.
gawk -v old="$old_text" -v new="$new_text" -v RS="^$" -i inplace '
{
found=index($0,old)
}
found{
print substr($0,1,found) new substr($0,found+length(old)+1)
found=""
next
}
' Input_file
Explanation: Adding detailed explanation for code.
gawk -v old="$old_text" -v new="$new_text" -v RS="^$" -i inplace ' ##Starting gawk program here mentioning variable named old whose value is of value of shell variable named old_text.
##New variable has new_text shell variable value in it. Now Setting RS(record separator as ^$) to make all lines to be treated as a single one.
{ ##Starting main BLOCK here.
found=index($0,old) ##using index function of awk which will provide index number of ay provided variable, here we want to know index(starting point) of variale old and saving it into found awk variable.
}
found{ ##Checking condition if vriable found is NOT NULL then do following.
print substr($0,1,found) new substr($0,found+length(old)+1) ##Printing substring from line 1st character to till index of variable old then printing new variable and again printing sub-string which will basically print everything after old variable, nothing should be removed unnecessarily.
found="" ##Nullifying found variable here.
next ##next will skip all further statements from here.
} ##Closing main BLOCK here.
' Input_file ##Mentioning Input_file name here.