РЕДАКТИРОВАТЬ: Поскольку OP прояснил вопрос больше, добавив код в соответствии с этим сейчас.
Предполагая, что следующим является 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
Теперь запустите следующий код.
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
Вывод будет следующим:
aaaaaa
bbbbbib
.
[%collapsible]
====
</div>
whwiuwviweivbw
wivuibwuivweiweg
wkvbwjvbwjbvwbviwrbhb
wvhwrivbwvbwrvbw
====
bfifiefe
fjbfiuebfiewfhbew
jwnjwnjwevbw
Не могли бы вы попробовать следующее, я проверил это с gawk
и с одним тестом Input_file, и он успешно работал, попросил бы вас проверить его с 1 Input_file один раз и довольный результатами, затем попробуйте его на *.html
файлах.
Сначала установите переменную текущего значения как old_text
Переменная оболочки:
old_text="+++ <details><summary> +++
some description
+++ </summary><div> +++
this
is
going
to be
folded
+++ </div></details> +++"
Теперь установите переменную оболочки с именем new_text
с новым текстовым значением, которое вы хотите заново в файле Input_file.
new_text=".some description
[%collapsible]
====
this
is
going
to be
folded
===="
Теперь запустите следующий код в файле 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
Объяснение: Добавление подробного пояснения к коду.
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.