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?

marked as duplicate by don_crissti, Jeff Schaller, dr01, roaima, cuonglm Mar 1 '16 at 12:33

This question was marked as an exact duplicate of an existing question.

  • 2
    @dhinchek welcome to U&L, please avoid asking the same question twice. can you delete one of them ? – Archemar Mar 1 '16 at 10:49
3
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.