Skip to main content

Replace column in one file with column from another using awk?

I have two files:

file1.txt:

111|aaa|444
222|bbb|555 
333|ccc|666

file2.txt:

111
(null)
333 

replacing column two of file1 with column one of file2

Expected Output

new:

111|111|444
222||555
333|333|666

I am using the below command,
awk 'BEGIN {FS=OFS="|"}NR == FNR {a[FNR] = $B;next}$A = a[FNR]' B=1 A=2 file2.txt file1.txt > new.txt

Output which I am getting,

new:

111|111|444
333|333|666

I am loosing the second record. how to avoid loss of record?

dhinchek
  • 21
  • 1
  • 5