使用另一个文件中的行范围替换单独文件中出现的字符串中的每2 n个 [英] Replace each 2 nth occurs from a string in separate files using line range from another file
本文介绍了使用另一个文件中的行范围替换单独文件中出现的字符串中的每2 n个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个文件:
0.txt
e0-1.txt
内容相同:
"#sun ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"#sun ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree(home_cool) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree(home_cool) ",
和源文件1.txt
如下:
(food, apple,)(bag, tortoise,)
(sky, cat,)(sun, sea,)
(car, shape)(milk, market,)
(man, shirt)(hair, life)
(dog, big)(bal, pink)
对于0.txt
,我想将从home_cool
开始出现的第2个替换为第1个1.txt
行,但最多只能使用1.txt
(然后sed -n '1,2p'
)的第二行,因此我的2.txt
输出如下:
"#sun ",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((sky, cat,)(sun, sea,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((sky, cat,)(sun, sea,)) ",
"machine(shoes_shirt.shop) ",
"#sun ",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((food, apple,)(bag, tortoise,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((sky, cat,)(sun, sea,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((sky, cat,)(sun, sea,)) ",
当在2.txt
完成该过程时,我想将home_cool
在0-1.txt
出现的第2 n全部替换为第1 n行,使用1.txt
的第三行(然后sed -n '3,5p'
),这样我的3.txt
输出如下:
"#sun ",
"car_snif = house.group_tree((car, shape)(milk, market,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((car, shape)(milk, market,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((man, shirt)(hair, life)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((man, shirt)(hair, life)) ",
"machine(shoes_shirt.shop) ",
"#sun ",
"car_snif = house.group_tree((dog, big)(bal, pink)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((dog, big)(bal, pink)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((car, shape)(milk, market,)) ",
"machine(shoes_shirt.shop) ",
"car_snif = house.group_tree((car, shape)(milk, market,)) ",
使用下面的行,我可以将home_cool
的替换分为两个步骤(第一步sed -n '1,2p'
和第二步sed -n '3,5p'
)。但我想将第一步保存在2.txt
中,将第二步保存在3.txt
中:
awk 'NR==FNR {a[NR]=$0; n=NR; next}/home_cool/ { gsub("home_cool", a[int((++i-1)%(n*2)/2)+1])}1' <(cat 1.txt | tee >(sed -n '1,2p') >(sed -n '3,5p')) 0.txt >> 2.txt
所以我真正想要的是(下面的伪代码):
awk 'NR==FNR {a[NR]=$0; n=NR; next}/home_cool/ { gsub("home_cool", a[int((++i-1)%(n*2)/2)+1])}1' <(cat 1.txt | tee >(sed -n '1,2p') >(sed -n '3,5p')) | "to sed -n '1,2p' make" 0.txt >> 2.txt | "to sed -n '3,5p' make" 0-1.txt >> 3.txt
如何通过维护命令行而不中断隔离的几个awk片段来完成此操作?
注意:也许问题的标题应该是:多个输入,相同的过程,不同的输出。
推荐答案
工作正常:
awk
'FNR==1 {++f}
f==1 {a[i++]=$0}
f==2 {if ($0~/home_cool/) {gsub(/home_cool/, a[int(j++/2)%2]) }; print > "2.txt"}
f==3 {if ($0~/home_cool/) {gsub(/home_cool/, a[int(k++/2)%3 + 2]) }; print > "3.txt"}'
1.txt 0.txt 0-1.txt
硬编码"2.txt"
和"3.txt"
的替代方案包括:
- 使用
-v outfile1=2.txt -v outfile2=2.txt
赋值的变量
- 替换为
outfile
,并使用此参数列表:1.txt outfile=2.txt 0.txt outfile=3.txt 0-1.txt
- 替换为
ARGV[4]
和ARGV[5]
,添加f==4 {exit}
行,使用参数列表:1.txt 0.txt 0-1.txt 2.txt 3.txt
注意事项:
- 如果给定的文件为空,则不会导致
f
递增,并相应地中断。在Gawk中,可以使用ENDFILE
。查看此答案:How to get the filenumber that is being processing by an awk script?
这篇关于使用另一个文件中的行范围替换单独文件中出现的字符串中的每2 n个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文