用提供的文件的数字相应的第 n 行一次替换第 n 次出现的 'foo' 和 'bar'
Replace each nth occurrence of 'foo' and 'bar' by numerically respective nth line of a supplied file once time
我想使用单个命令行(我尝试使用管道和进程替换来执行此操作但未成功,如下所述)来替换第 n 次出现的 home_cool
和 home_cool01
)所提供文件的第 n 行的数字,最好不使用管道,而是添加 home_cool01
作为命令中获取的另一个模式。
知道 0.txt
包含 2 次 的 home_cool
和 2 次 的 home_cool01
.
我有这个命令(按顺序使用)用提供的文件的第n行分别替换第n次出现的home_cool
和home_cool01
1.txt
如下:
awk 'NR==FNR {a[NR]=[=12=]; next} /home_cool/{gsub("home_cool", a[++i])} 1' 1.txt 0.txt > 2.txt
和
awk 'NR==FNR {a[NR]=[=13=]; next} /home_cool01/{gsub("home_cool01", a[++i])} 1' 1.txt 2.txt > 3.txt
但我不想要两个单独的命令,即使使用 pipe
。
这是我的文件:
文件 0.txt
如下:
"#sun\t",
"\t",
"bread = door('dog', 'paint')\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"home_cool\")\t",
"\t",
"car_snif = house.group_tree(home_cool)\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"home_cool01\")\t",
"\t",
"car_snif = house.group_tree(home_cool01)\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\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",
"\t",
"bread = door('dog', 'paint')\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"(food, apple, sky, cat,blue,)(bag, tortoise,)\")\t",
"\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"(food, apple, sky, cat,blue,)(bag, tortoise,)\")\t",
"\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\t",
注意:我可以将此问题视为的子问题
我想过通过聊天或评论来帮助这个子问题的作者,但我相信这是一个需要完成的新问题,所以我在 OS 上打开了一个新的 post。
请您尝试以下操作:
awk 'NR==FNR {a[NR]=[=10=]; next}
/home_cool01/ {gsub("home_cool01", a[++i])}
/home_cool/ {gsub("home_cool", a[++j])} 1' 1.txt 0.txt > 2.txt
- 为了便于阅读,我将命令分成三行,但您可以
将它们合并成一行。
- 两个变量
i
和 j
分别对每个数值起作用
引用。
- 与
home_cool01
的匹配需要先测试,否则
home_cool
将匹配 home_cool01
. 的子字符串
我想使用单个命令行(我尝试使用管道和进程替换来执行此操作但未成功,如下所述)来替换第 n 次出现的 home_cool
和 home_cool01
)所提供文件的第 n 行的数字,最好不使用管道,而是添加 home_cool01
作为命令中获取的另一个模式。
知道 0.txt
包含 2 次 的 home_cool
和 2 次 的 home_cool01
.
我有这个命令(按顺序使用)用提供的文件的第n行分别替换第n次出现的home_cool
和home_cool01
1.txt
如下:
awk 'NR==FNR {a[NR]=[=12=]; next} /home_cool/{gsub("home_cool", a[++i])} 1' 1.txt 0.txt > 2.txt
和
awk 'NR==FNR {a[NR]=[=13=]; next} /home_cool01/{gsub("home_cool01", a[++i])} 1' 1.txt 2.txt > 3.txt
但我不想要两个单独的命令,即使使用 pipe
。
这是我的文件:
文件 0.txt
如下:
"#sun\t",
"\t",
"bread = door('dog', 'paint')\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"home_cool\")\t",
"\t",
"car_snif = house.group_tree(home_cool)\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"home_cool01\")\t",
"\t",
"car_snif = house.group_tree(home_cool01)\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\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",
"\t",
"bread = door('dog', 'paint')\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"(food, apple, sky, cat,blue,)(bag, tortoise,)\")\t",
"\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\t",
"door = world.sea\t",
"world.sea = bread\t",
"\t",
"machine(\"(food, apple, sky, cat,blue,)(bag, tortoise,)\")\t",
"\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"\t",
"machine(shoes_shirt.shop)\t",
"machine(car_snif.car_snif)\t",
注意:我可以将此问题视为
请您尝试以下操作:
awk 'NR==FNR {a[NR]=[=10=]; next}
/home_cool01/ {gsub("home_cool01", a[++i])}
/home_cool/ {gsub("home_cool", a[++j])} 1' 1.txt 0.txt > 2.txt
- 为了便于阅读,我将命令分成三行,但您可以 将它们合并成一行。
- 两个变量
i
和j
分别对每个数值起作用 引用。 - 与
home_cool01
的匹配需要先测试,否则home_cool
将匹配home_cool01
. 的子字符串