Skip to main content
don't print the lines from 1st file
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

Assuming your input files are tab-delimited:

$ join -a 1 -e 0 -t $'\t' -o 1.1,2.2 F1.txt F2.txt A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 

join the two files, ensure all lines in the 1st file are present (-a), if any fields are null then use the value "0" (-e), take the first field from the first file and the 2nd field from the 2nd file (-o), and use tab as the delimiter (-t)

If you want awk, I'd write (note the order of the file arguments)

awk 'BEGIN {FS=OFS="\t"} NR==FNR {v[$1]=$2v[$1]=$2; next} {print $1, (v[$1] ? v[$1] : 0)}' F2.txt F1.txt 

Assuming your input files are tab-delimited:

$ join -a 1 -e 0 -t $'\t' -o 1.1,2.2 F1.txt F2.txt A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 

join the two files, ensure all lines in the 1st file are present (-a), if any fields are null then use the value "0" (-e), take the first field from the first file and the 2nd field from the 2nd file (-o), and use tab as the delimiter (-t)

If you want awk, I'd write (note the order of the file arguments)

awk 'BEGIN {FS=OFS="\t"} NR==FNR {v[$1]=$2} {print $1, (v[$1] ? v[$1] : 0)}' F2.txt F1.txt 

Assuming your input files are tab-delimited:

$ join -a 1 -e 0 -t $'\t' -o 1.1,2.2 F1.txt F2.txt A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 

join the two files, ensure all lines in the 1st file are present (-a), if any fields are null then use the value "0" (-e), take the first field from the first file and the 2nd field from the 2nd file (-o), and use tab as the delimiter (-t)

If you want awk, I'd write (note the order of the file arguments)

awk 'BEGIN {FS=OFS="\t"} NR==FNR {v[$1]=$2; next} {print $1, (v[$1] ? v[$1] : 0)}' F2.txt F1.txt 
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Assuming your input files are tab-delimited:

$ join -a 1 -e 0 -t $'\t' -o 1.1,2.2 F1.txt F2.txt A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 

join the two files, ensure all lines in the 1st file are present (-a), if any fields are null then use the value "0" (-e), take the first field from the first file and the 2nd field from the 2nd file (-o), and use tab as the delimiter (-t)

If you want awk, I'd write (note the order of the file arguments)

awk 'BEGIN {FS=OFS="\t"} NR==FNR {v[$1]=$2} {print $1, (v[$1] ? v[$1] : 0)}' F2.txt F1.txt