【发布时间】:2021-12-20 08:17:45
【问题描述】:
我正在尝试使用以下command_line01 将home_cool 的第一次和第二次出现分别替换为1.txt 的第一行,然后继续替换home_cool01 的第一次和第二次出现也分别由1.txt 的第一行,然后..,用1.txt 的第二行单独替换第三和第四次出现的home_cool。等等,即每2 nth 出现home_cool 或home_cool01,分别用1.txt 的第n 行替换两个字符串。
我试过command_line01 波纹管:
awk 'NR==FNR {a[NR]=$0; 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_cool 或 home_cool01 被 nothing 替换,如下所示:
"#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 cmets:
1 - 如果有多个条目,如果已经完成第一个条目的处理,我将从第二个重新启动。
2 - 如果我有超过 6 个 home_coool 条目...我从 1.txt 的开头继续。
3 - 我不想仅限于两种研究模式,所以当有home_coool02、home_coool03、...、home_coool_some_sufix 时,它应该是一个合适的解决方案,但我需要保持我最初发布的 MWE
【问题讨论】:
-
如果输入是
home_cool、home_cool01、home_cool怎么办...您是重新启动第二个home_cool的计数器还是从上次中断的地方继续?如果您有超过 6 个home_cool条目会发生什么...您是继续使用来自1.txt的最后一个条目还是从1.txt的开头重新开始?您是否需要担心其他搜索模式,例如home_cool02、home_cool04、home_cool_some_other_suffix等? -
我认为您可能需要阅读有关 % 运算符的信息。
-
@Luuk 我会阅读谢谢!
-
@markp-fuso 对您的第一条评论: 1 - 如果有多个条目,如果已经完成第一个条目的处理,我将从第二个条目重新启动。 2 - 如果我有超过 6 个 home_coool 条目......我从
1.txt的开头继续。 3 - 我不想仅限于两种研究模式,所以当有home_coool02,home_coool03,...,home_coool_some_sufix时,它应该是一个合适的解决方案,但我需要保持一个MWE作为我最初发布了