注意:我已经在AWK 中问过一个类似的问题:Quick way to insert target words after an source term,我是 AWK 的初学者。
这个问题考虑在随机选择的行中在源词之后插入多个目标词。
有了这个 AWK 代码片段
awk '(NR==FNR){a[$1];next}
FNR in a { gsub(/\<source term\>/,"& target term") }
1
' <(shuf -n 5 -i 1-$(wc -l < file)) file
我想target term
在.source term
file
例如:我有一个双语词典dict
,其中包含左侧的源术语和右侧的目标术语,例如
apple : Apfel
banana : Banane
raspberry : Himbeere
我的file
由以下几行组成:
I love the Raspberry Pi.
The monkey loves eating a banana.
Who wants an apple pi?
Apple pen... pineapple pen... pen-pineapple-apple-pen!
The banana is tasty and healthy.
An apple a day keeps the doctor away.
Which fruit is tastes better: raspberry or strawberry?
假设第一个单词apple
随机选择第 1、3、5、4、7 行。带有单词 apple 的输出将如下所示:
I love the Raspberry Pi.
The monkey loves eating a banana.
Who wants an apple Apfel pi?
Apple Apfel pen... pineapple pen... pen-pineapple-apple-pen!
The banana is tasty and healthy.
An apple a day keeps the doctor away.
Which fruit is tastes better: raspberry or strawberry?
然后是另外 5 条随机线;3、3、5、6、7;对于单词banana
将被选中:
I love the Raspberry Pi .
The monkey loves eating a banana .
Who wants an apple Apfel pi ?
Apple Apfel pen... pineapple pen... pen-pineapple-apple-pen!
The banana Banane is tasty and healthy .
An apple a day keeps the doctor away .
Which fruit is tastes better: raspberry or strawberry?
dict
在匹配最后一个条目之前,所有其他条目也是如此。
我想选择 5 条随机线。如果这些行有一个完整的源术语,比如我apple
只想匹配整个单词(诸如“菠萝”之类的术语将被忽略)。如果一行包含两次源术语,例如,那么我也想在它之后插入目标术语。匹配应该不区分大小写,所以我也可以匹配源术语,比如and 。Apfel
apple
apple
apple
Apple
我的问题:我怎样才能重写上面的代码片段,这样我就可以使用字典dict
,它选择随机行file
并在源术语后面插入目标术语?