【问题标题】:replacing a syntax structure in all files and subfolder of a code base替换代码库的所有文件和子文件夹中的语法结构
【发布时间】:2020-02-22 23:59:16
【问题描述】:

this post之后,我想替换所有的HTML结构:

+++ <details><summary> +++
some description
+++ </summary><div> +++
this
is
going
to be
folded
+++ </div></details> +++

使用原生 AsciiDoc

.some description
[%collapsible]
====
this
is
going
to be
folded
====

在一个文件夹及其所有子文件夹的所有文件中。如果我想替换单个字符串,那么我可以使用this page 中的任何方法,但这里我有一个内部包含其他内容的结构。如果您能帮助我了解最规范/最有效的方法,我将不胜感激。

P.S. 我想我的问题很清楚,但为了澄清起见,我不想替换上面的两个字符串,而是替换结构。换句话说:

  1. +++ &lt;details&gt;&lt;summary&gt; +++\n --> .
  2. +++ &lt;/summary&gt;&lt;div&gt; +++ --> [%collapsible]\n====
  3. +++ &lt;/div&gt;&lt;/details&gt; +++ --> ====

我可以在三轮内替换这些,但我想学习如何做到一次。

P.S.2.我的问题与this one非常相似。

P.S.3. 正则表达式模式应该类似于

(\+{3}\s*<details>[\S\s]*?<summary>\s*\+{3})[\S\s]*?(\+{3}\s*<\/summary>[\S\s]*?<div>\s*\+{3})[\S\s]*?(\+{3}\s*<\/div>[\S\s]*?<\/details>\s*?\+{3})

但是我无法让sed 工作。这是我能做到的:

find . -type f -name "*.adoc" -o -name "*.sci" | xargs sed -n -E '/(\+{3} <details><summary> \+{3})/p'

【问题讨论】:

  • 欢迎来到 Stack Overflow。 SO 是面向专业和热情的程序员的问答页面。将您自己的代码添加到您的问题中。您应该至少展示自己为解决这个问题所做的研究。
  • 亲爱的@Cyrus 我对 SO 并不陌生。我尽量不使帖子混乱,因为根据我的经验,它会使其他人感到困惑。但在另一篇文章中,我已经链接了我已经提到过我尝试过的正则表达式模式。除此之外,我没有其他结果,因为我不知道要搜索的正确关键字。

标签: regex bash awk sed grep


【解决方案1】:

编辑:由于 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.

【讨论】:

  • 我正在努力理解您的回答。我知道一点正则表达式,但从未使用过 gawk tbh。您是如何识别我在帖子中提到的 HTML 代码结构的?
  • @Foad,我已经添加了详细的解释,如果有任何疑问,请告诉我。
  • 对不起,如果我的问题看起来很粗鲁,但您是否认为我想用另一个字符串替换一个字符串?因为我提到的结构的内部只是任意文本。例如,从不在文件中,有一个字符串“一些描述”。我想替换结构,而不是字符串。这就是我标记正则表达式的原因。
  • @Foad,对不起,如果我没有得到你的问题。它不是单个字符串,它是您可以在字符串中提及的行数(1 需要替换,1 需要新放在那里)。如果不是这种情况,请让我们用更多的例子更清楚地讨论这个问题。
  • 我编辑了帖子以进行澄清。基本上我不想用另一个替换多行字符串,而是用 AsciiDoc 替换那些任意内容周围的 HTML 结构。
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 2023-03-27
  • 1970-01-01
  • 2013-05-30
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
相关资源
最近更新 更多