每 2 个字符串分别出现第 n 次,用文件的第 n 行替换两个不同的字符串

replace two different strings with the nth line of a file every 2 nth occurrences of each string separately

我正在尝试使用以下 command_line01 将第一次和第二次出现的 home_cool 分别替换为 1.txt 的第一行,然后继续,替换第一次和第二次出现的 home_cool01 也由 1.txt 的第一行单独出现,然后..,用 1.txt 的第二行替换第三次和第四次出现的 home_cool等,即每 2 nth 出现的 home_coolhome_cool01,分别用 1.txt 的第 n 行替换两个字符串.

我试过 command_line01 如下:

awk 'NR==FNR {a[NR]=[=10=]; next} /home_cool01/{gsub("home_cool01", a[++i<3])} /home_cool/{gsub("home_cool", a[++j<3])} 1' 1.txt 0.txt > 2.txt

但这只适用于前两次出现接下来的两次出现home_coolhome_cool01替换[无] =63=] 如下所示:

"#sun\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",

这是我的两个源文件:

0.txt:

"#sun\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tre(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",

1.txt:

(food, apple, sky, cat,blue,)(bag, tortoise,)
(food, apple, sky, cat,blue,)(bag,)
(food, apple, sky, cat,blue,)(bag, moon, tortoise,)

我的愿望输出2.txt是:

"#sun\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",

编辑: 来自@markp-fuso 评论:

1 - 如果有多个条目,如果第一个条目已经完成处理,我将从第二个重新启动。

2 - 如果我有超过 6 个 home_coool 条目...我从 1.txt.

的开头继续

3 - 我不想只局限于两种研究模式,所以当有 home_coool02, home_coool03 , ..., [=31 时,它应该是一个合适的解决方案=],但我需要保留我最初发布的 MWE

假设:

  • 只需要担心搜索模式 home_coolhome_cool01(可以添加更多但需要一些返工;可能使用关联数组来跟踪每个独特模式的计数)
  • 在进入下一个替换模式之前,将应用替换模式两次
  • 如果我们到达替换模式的末尾,我们会从头开始

示例输入:

$ cat 0.txt
"#sun\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",          # pick up where we left off with 'home_cool'
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",          # restart from beginning of 0.txt replacment patterns
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",

一个awk想法:

awk '
NR==FNR       { a[NR]=[=11=]; n=NR; next}
/home_cool01/ { gsub("home_cool01", a[int((i++)%(n*2)/2)+1])}
/home_cool/   { gsub("home_cool",   a[int((j++)%(n*2)/2)+1])}
1
' 1.txt 0.txt

其中:

  • n==3 - 1.txt 中的行数;所以 n*2 == 6
  • %6 - 将生成 0-5
  • 的输出
  • (%6/2) - 将输出变为 0.0, 0.5, 1.0, 1.5, 2.0, 2.5
  • int(...) - 将输出变为 0, 0, 1, 1, 2, 2
  • +1 - 为我们提供 1, 1, 2, 2, 3, 3
  • 的数组索引
  • 注意: 是的,索引有点复杂;我愿意接受简化的建议

这会生成:

"#sun\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, moon, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, moon, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",