1

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?

1
  • I'm voting to reopen this as the duplicate question was deleted.
    – AdminBee
    Oct 20, 2021 at 14:53

2 Answers 2

4
awk 'BEGIN {FS=OFS="|"}NR == FNR {a[FNR] = $B;next}{$A = a[FNR];print $0}' B=1 A=2 f2 f1
0

Alternatively you can use paste and cut for this:

paste -d'|' <(cut -d'|' -f1 file1) <(cat file2) <(cut -d'|' -f3 file2)

I cannot tell which is better in terms of speed, though.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .