【发布时间】:2021-12-21 17:53:01
【问题描述】:
我实际上想使用 nth 行替换 0.txt 文件上的两个字符串之间的任何内容,)\t", 每隔 nth 行从另一个文件 1.txt 到 awk。
这类似于Replace each nth occurrence of 'foo' by numerically respective nth line of a supplied file
我一直在寻找一些东西,我试图适应这个https://stackoverflow.com/a/21876700/10824251,但我不知道它如何适用于我所寻找的东西。这是我的尝试:
awk \
'NR==FNR {a[NR]=$0; next} /^group_tree(/ /{gsub("tortoise", a[++i])} /^)\t",/1' \
1.txt 0.txt
不要只生成消息的任何结果:
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)
.....
我的源文件:
0.txt:
"#sun\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"#sun\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\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:
"#sun\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((car, shape)(milk, market,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((man, shirt)(hair, life))\t",
"machine(shoes_shirt.shop)\t",
"#sun\t",
"car_snif = house.group_tree((dog, big)(bal, pink))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((sky, cat,)(sun, sea,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((car, shape)(milk, market,))\t",
【问题讨论】:
-
您输入中的
\ts 是字面上的 2 个字符\和t还是您输入中实际上有文字制表符? -
在您的问题文本中您说
group_tree (带有空格,但在您的示例输入/输出中没有这样的空格。请edit您的问题使文本和示例保持一致和正确。
标签: string file awk replace gsub