awk - 使用来自另一个文件的行范围从不同文件中的两个字符串之间的任何内容替换每个 X nth

awk - Replace each X nth occurs from anything between two strings in Different files using line range from another file

我想用另一个文件中的行范围替换不同文件中两个字符串 group_tree(\t 之间出现的每个 X nth。

我在理解这个问题的操作,我重温了作者的解释。

但我没有成功,我仍然难以处理它以某种方式说 [a[int(n++/2)%2+4]a[int(j++/2)%2+0] 的段落(其中,我知道例如 0 表示第一行索引 1.txt 用于替换 0.txt 处的任何内容,但其他元素不确定它们的确切作用。

例如,在下面以f==4开头的行中,如果我设置替换开始使用1.txt的最后一行(index 4,即[int (n++/2)%2 + 4] ),输出文件 4.txt 没有预期的内容,如下所示:

"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree()\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree()\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree()\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree()\t"

而不是:

"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"

另一个困难是我只能替换0.txt0-1.txt0-2.txt的第2 n个连续行,例如我不能替换[的前3个连续行=26=] 使用第一个 1.txt 行替换。我试图通过将数字 3 放在下面代码第四行的 a[int (j++/2)%3 + 0 上来做到这一点:

awk \
'FNR==1 {++f}
f==1 {a[i++]=[=12=]}
f==2 {if (sub(/group_tree[[:space:]]*\(.*\)\t",$/,"group_tree("a[int(j++/2)%3+0]")\t\"")) {j++}; print > "2.txt"}
f==3 {if (sub(/group_tree[[:space:]]*\(.*\)\t",$/,"group_tree("a[int(k++/2)%2+3]")\t\"")) {k++}; print > "3.txt"}
f==4 {if (sub(/group_tree[[:space:]]*\(.*\)\t",$/,"group_tree("a[int(n++/2)%2+4]")\t\"")) {n++}; print > "4.txt"}
' \
    1.txt 0.txt 0-1.txt 0-2.txt

但这产生了下面的 2.txt

"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((car, shape)(milk, market,))\t"
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((car, shape)(milk, market,))\t"
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"

而不是:

"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((car, shape)(milk, market,))\t"
"car_snif = house.group_tree((car, shape)(milk, market,))\t"

来自源文件 0.txt0-1.txt0-2.txt 的内容相同:

"car_snif = house.group_tree((food,hhhh))\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"car_snif = house.group_tree((foodgggggtoise,))\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",

来自 1.txt 的内容是:

(food, apple,)(bag, tortoise,)
(sky, cat,)(sun, sea,)
(car, shape)(milk, market,)
(man, shirt)(hair, life)
(dog, big)(bal, pink)

编辑更新:

所需输出:

2.txt:

"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t"
"car_snif = house.group_tree((car, shape)(milk, market,))\t"
"car_snif = house.group_tree((car, shape)(milk, market,))\t"

3.txt:

"car_snif = house.group_tree((man, shirt)(hair, life))\t"
"car_snif = house.group_tree((man, shirt)(hair, life))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((man, shirt)(hair, life))\t"
"car_snif = house.group_tree((man, shirt)(hair, life))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"

4.txt:

"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"
"car_snif = house.group_tree((dog, big)(bal, pink))\t"

成为a[int((k++)% (x3*2)/x2)+x1]: 是 N 的总行数 1.txtx1是我希望在1.txt的第一行使用的行索引(大于等于0,小于等于N)替换。

x2是希望被1.txt的第nth行替换的出现次数,换句话说x2是我使用 1.txt 的某一行在 0.txt0-1.txt0-2.txt.

处进行替换的重复次数

现在,假设我想使用包含 x1 索引行加上 Z 行的间隔(Z 小于或等于“N - (x1 + 1)”) ,以及 Z + 1 = U = (x3*2)/x2。对于 (x3*2)/x2 我们知道 x2 是常数,因为它代表我选择使用相同 1.txt 行的次数,然后根据安排 x3*2 这样在最后(x3*2)/x2 = U.

此时 x3 可以在代数上找到,也可以在 %(x3*2)/x2 的派生序列中找到,如@markp-fuso 的解释(此处使用 (%6/2)).

只要 U 永远不会大于 N 那么 group_tree 之间就永远不会有空格(和 2.txt 中的 \t3.txt4.txt 输出文件,所以我认为问题已解决恕我直言。

如果我的回答非常错误,我愿意更正或删除。现在我会等待一段时间,我不会标记为已解决,因为我的答案虽然对我有用,但我不是专家,我等待专家更学术的答案。

awk \
'FNR==1 {++f}
f==1 {a[i++]=[=10=]}
f==2 {if (sub(/group_tree[[:space:]]*\(.*\)\t",$/,"group_tree("a[int((h++)%(3*2)/2)+1]")\t\"")) ; print > "2.txt"}
f==3 {if (sub(/group_tree[[:space:]]*\(.*\)\t",$/,"group_tree("a[int((j++)%(1*1)/1)+4]")\t\"")) ; print > "3.txt"}
f==4 {if (sub(/group_tree[[:space:]]*\(.*\)\t",$/,"group_tree("a[int((k++)%(3*2)/2)+2]")\t\"")) ; print > "4.txt"}' \
    1.txt 0.txt 0-1.txt 0-2.txt

注意:这里我使用了三个不同的变量,@markp-fuso 在评论中也观察到了这一点。变量分别为hjk。这是避免增量重叠所必需的(即 j++k++h++)。另请注意,Ux1 是相关的,一个的值影响另一个输出文件的最终结果,在本例中,2.txt3.txt4.txtU 的不合适值导致在输出文件内容中搜索的两个字符串之间出现空格。

解释:

代码中的第 4 行:使用索引行 13 之间的第 n 1.txt 行,2 次每次出现两次搜索之间的任何内容字符串

代码中的第 5 行:使用第 n 行 1.txt,在这种情况下,间隔仅由索引行 41.txt 的最后一行)组成,1 次每次出现两个搜索字符串之间的任何内容。

代码中的第 6 行:使用索引 24 行之间的第 n 1.txt 行,对两个搜索字符串之间的任何内容每次出现 2 次。