Skip to main content
Post Reopened by AdminBee, Philippos, Stephen Kitt, muru, αғsнιη awk

I have two files:

f1file1.txt:

111|aaa|444

222|bbb|555

333|ccc|666

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

f2file2.txt:

111

(null)

333

111
(null)
333 

replacing column 2two of f1file1 with column 1one of f2file2

required outputExpected Output

new:

111|111|444

222||555

333|333|666

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

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

what iOutput which I am getting,

new:

111|111|444

333|333|666

111|111|444
333|333|666

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

I have two files:

f1:

111|aaa|444

222|bbb|555

333|ccc|666

f2:

111

(null)

333

replacing column 2 of f1 with column 1 of f2

required output

new:

111|111|444

222||555

333|333|666

i am using this command awk 'BEGIN {FS=OFS="|"}NR == FNR {a[FNR] = $B;next}$A = a[FNR]' B=1 A=2 f2.txt f1.txt > new.txt

what i am getting

new:

111|111|444

333|333|666

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

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?

Source Link
dhinchek
  • 21
  • 1
  • 5

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

I have two files:

f1:

111|aaa|444

222|bbb|555

333|ccc|666

f2:

111

(null)

333

replacing column 2 of f1 with column 1 of f2

required output

new:

111|111|444

222||555

333|333|666

i am using this command awk 'BEGIN {FS=OFS="|"}NR == FNR {a[FNR] = $B;next}$A = a[FNR]' B=1 A=2 f2.txt f1.txt > new.txt

what i am getting

new:

111|111|444

333|333|666

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