问题描述
好吧,我再也没有为您中的一位奇才而烦恼了,我一直在与awk一起玩耍,还没有弄清楚这个问题.因此,无需再拖延,这就是我要解决的问题.
Ok im back with another no brainer for one of you wonderful wizards, Ive been playing around with awk, and havent quite got this figured out yet. So without further delay, here is the problem I am trying to solve.
我有两个文件
file1看起来像这样(实际文件有数百行,带有随机单词)
file1 looks like this ( actual file has hundreds of lines w random words )
somewebsite
someotherwebsite
somestinking
blahblah
foobar
file2看起来像这样(许多tld,还有更多)
file2 looks something like this ( many tlds, a lot more )
.com.th
.co.uk
.com
.de
.ath.cx
好吧,我需要file1中的每一行在新行中从file2中添加每个tld....
Ok, I need each line in file1 to have each tld added from file2 on a new line....
为了进一步详细说明,需要复制file1中的每一行,以便可以将file2中的每个tld添加到file1中的每个条目中.
To further elaborate, each line in file1 needs to be replicated so that it can have every tld from file2 added to every entry in file1.
输出应如下所示:
somewebsite.com.th
somewebsite.co.uk
somewebsite.com
somewebsite.de
somewebsite.ath.cx
someotherwebsite.com.th
someotherwebsite.co.uk
someotherwebsite.com
someotherwebsite.de
someotherwebsite.ath.cx
somestinking.com.th
somestinking.co.uk
somestinking.com
somestinking.de
somestinking.ath.cx
blahblah.com.th
blahblah.co.uk
blahblah.com
blahblah.de
blahblah.ath.cx
foobar.com.th
foobar.co.uk
foobar.com
foobar.de
foobar.ath.cx
我希望这对某人有意义,我试图弄清楚该怎么做,这肯定使我失败的所有方式都有趣.
I hope that makes sense to somebody, Im trying to figure out how to do it, its certainly amusing all the ways I have failed.
先谢谢您.我确定我不是现在,过去或将来尝试过此操作的唯一人,因此,解决方案一定会帮助下一个尝试执行此操作的人.
Thank you in advance. Im sure I am not the only person who has tried this now, in the past, or in the future, so a solution will certainly help the next person attempting to do this.
推荐答案
在awk中:
$ awk 'NR==FNR{a[$1];next}{for(i in a) print $1 i}' file2 file1
somewebsite.co.uk
somewebsite.de
somewebsite.com
somewebsite.ath.cx
somewebsite.com.th
...
由于in
运算符的性质,tlds出现的顺序是随机的.
The order the tlds come out is random due to the nature of in
operator.
或仅使用join
(和tr
):
$ join -j 2 file1 file2 | tr -d ' '
somewebsite.com.th
somewebsite.co.uk
somewebsite.com
somewebsite.de
...
这篇关于对于每一行,创建一个新行以附加来自另一个文件的多个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!