1

I have two files--file1 and file2 that I want to join but some fields are missing in second file for which I want to insert string 'null'. One requirement is that the keys must be in the same order as in file1.

The input files and expected output result are as below:

file1.txt    file2.txt
a 7 nah      a anau
b 0 blah     c bau
c 5 bah      d cau
d 1 gah
e 0 hah

Expected output result:

a 7 nah anau
b 0 blah null
c 5 bah bau
d 1 gah cau
e 0 hah null
αғsнιη
  • 40,939
  • 15
  • 71
  • 114
Ketan Maheshwari
  • 9,054
  • 6
  • 40
  • 53

3 Answers3

3

join + sort solution:

join -o1.1,1.2,1.3,2.2 -a1 -e"null" <(sort file1.txt) <(sort file2.txt)

The output:

a 7 nah anau
b 0 blah null
c 5 bah bau
d 1 gah cau
e 0 hah null
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
1

Solution using join:

join file1.txt file2.txt -e null -o auto -a 1 2>/dev/null

It isn't clear from the man pages, but the -e option only works with -o.

m0dular
  • 1,231
  • 7
  • 8
1

I was able to solve it with awk. I found that in my data as in the example, the second field of file1.txt has to be 0 for it to be missing in file2.txt--which helped. The script looks like so:

NR==FNR {
  a[$1]=$2;next
}

$2!=0{
  print $0,a[$1]
}

$2==0{
  print $0,"null"
}

Invocation:

awk -f merge.awk file2.txt file1.txt
Ketan Maheshwari
  • 9,054
  • 6
  • 40
  • 53