Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

bash - For each line, create a new line to append with mutliple entries from another file

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.

I have two files

file1 looks like this ( actual file has hundreds of lines w random words )

somewebsite
someotherwebsite
somestinking
blahblah
foobar

file2 looks something like this ( many tlds, a lot more )

.com.th
.co.uk
.com
.de
.ath.cx

Ok, I need each line in file1 to have each tld added from file2 on a new line....

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.

Output should be something like this:

   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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In 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
...

The order the tlds come out is random due to the nature of in operator.

Or just use join (and tr):

$ join  -j 2 file1 file2 | tr -d ' '
somewebsite.com.th
somewebsite.co.uk
somewebsite.com
somewebsite.de
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...