编辑:由于 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_textshell变量:
old_text="+++ <details><summary> +++
some description
+++ </summary><div> +++
this
is
going
to be
folded
+++ </div></details> +++"
现在在 Input_file(s) 中设置名为 new_text 的新文本值的 shell 变量。
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.