【问题标题】:replace two different strings with the nth line of a file every 2 nth occurrences of each string separately每个字符串每出现第 2 次,分别用文件的第 n 行替换两个不同的字符串
【发布时间】:2021-12-20 08:17:45
【问题描述】:

我正在尝试使用以下command_line01home_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]=$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_coolhome_cool01nothing 替换,如下所示:

"#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_coool02home_coool03、...、home_coool_some_sufix 时,它应该是一个合适的解决方案,但我需要保持我最初发布的 MWE

【问题讨论】:

  • 如果输入是home_coolhome_cool01home_cool 怎么办...您是重新启动第二个home_cool 的计数器还是从上次中断的地方继续?如果您有超过 6 个 home_cool 条目会发生什么...您是继续使用来自 1.txt 的最后一个条目还是从 1.txt 的开头重新开始?您是否需要担心其他搜索模式,例如home_cool02home_cool04home_cool_some_other_suffix 等?
  • 我认为您可能需要阅读有关 % 运算符的信息。
  • @Luuk 我会阅读谢谢!
  • @markp-fuso 对您的第一条评论: 1 - 如果有多个条目,如果已​​经完成第一个条目的处理,我将从第二个条目重新启动。 2 - 如果我有超过 6 个 home_coool 条目......我从 1.txt 的开头继续。 3 - 我不想仅限于两种研究模式,所以当有home_coool02home_coool03,...,home_coool_some_sufix时,它应该是一个合适的解决方案,但我需要保持一个MWE作为我最初发布了

标签: string awk gsub


【解决方案1】:

假设:

  • 只需要担心搜索模式 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]=$0; 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",

【讨论】:

  • 我正在做测试,现在效果很好,感谢您的解释
  • 你的解释帮助我理解了其他 UNIX 操作,很好的解释..
  • 我有一个新需求:假设不是用 nth 的 1.txt 文件行替换 0.txt 文件模式中的 nth's,现在我想要 0.txt 和 @ 之间的过程987654342@ 只发生在1.txt 的第二行用作输出之前,此时我将进程重定向到另一个包含要替换的home_cool 字符串的文件。我应该为此发布一个新问题吗?
  • 是的,听起来像是一个新问题,特别是因为我(还)不明白你的提议......所以我假设会有更多轮次的来回......这只会使这个特定的问答变得混乱;是的,如果您打算将另一个文件加入其中,添加逻辑以在替换文件之间跳转……绝对是一个全新的问答
  • @mark-fuso 是的,我需要用更好的方式表达这一点,我将花一些时间来处理这个问题。
猜你喜欢
  • 2018-08-11
  • 2018-03-24
  • 2017-12-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
相关资源
最近更新 更多