Search code examples
bashshellconcatenation

Need to combine 2 files having different word list size


I need to combine 2 files of bash script that are having word lists with different word counts and I want to combine them as shown below.

File 1:

word1
word2
word3

File 2:

8.8.8.8
4.4.4.4
4.4.2.2
5.5.5.5

Desired Output:

word1,8.8.8.8
word1,4.4.4.4
word1,4.4.2.2
word1,5.5.5.5
word2,8.8.8.8
word2,4.4.4.4
word2,4.4.2.2
word2,5.5.5.5
word3,8.8.8.8
word3,4.4.4.4
word3,4.4.2.2
word3,5.5.5.5

Solution

  • Find a high enough field number (like 100) that is not contained in your files and (ab)use join to produce the cartesian product

    join -j 100 file1.txt file2.txt
    
     word1 8.8.8.8
     word1 4.4.4.4
     word1 4.4.2.2
     word1 5.5.5.5
     word2 8.8.8.8
     word2 4.4.4.4
     word2 4.4.2.2
     word2 5.5.5.5
     word3 8.8.8.8
     word3 4.4.4.4
     word3 4.4.2.2
     word3 5.5.5.5
    

    Edit: In order to have a comma as column separator, name it using the -t option, and to have the output not start with that separator (previously a space, now the comma), make the ordering explicit using the -o option:

    join -j 100 -t, -o 1.1,2.1 file1.txt file2.txt
    
    word1,8.8.8.8
    word1,4.4.4.4
    word1,4.4.2.2
    word1,5.5.5.5
    word2,8.8.8.8
    word2,4.4.4.4
    word2,4.4.2.2
    word2,5.5.5.5
    word3,8.8.8.8
    word3,4.4.4.4
    word3,4.4.2.2
    word3,5.5.5.5