Skip to main content
edited tags
Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262
formatting
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

I have two files.

I want to match the 1st column of first file to 1st column of second file. If there is a match I'd like to print the 2st column of second file, if not 0.

I have tried awk (awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txtawk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txt) but it is not working.

F1.txt

A

B

C

D

E

F

G

H

I

J

A B C D E F G H I J 

F2.txt

A 0.5

E 1

H 0.5

J 1

A 0.5 E 1 H 0.5 J 1 

Desired_output

A 0.5

B 0

C 0

D 0

E 1

F 0

G 0

H 0.5

I 0

J 1

A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 

I have two files.

I want to match the 1st column of first file to 1st column of second file. If there is a match I'd like to print the 2st column of second file, if not 0.

I have tried awk (awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txt) but it is not working.

F1.txt

A

B

C

D

E

F

G

H

I

J

F2.txt

A 0.5

E 1

H 0.5

J 1

Desired_output

A 0.5

B 0

C 0

D 0

E 1

F 0

G 0

H 0.5

I 0

J 1

I have two files.

I want to match the 1st column of first file to 1st column of second file. If there is a match I'd like to print the 2st column of second file, if not 0.

I have tried awk (awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txt) but it is not working.

F1.txt

A B C D E F G H I J 

F2.txt

A 0.5 E 1 H 0.5 J 1 

Desired_output

A 0.5 B 0 C 0 D 0 E 1 F 0 G 0 H 0.5 I 0 J 1 
Source Link
Mandy
  • 41
  • 2

Awk compare 2 files, print match together with non match lines filled with 0

I have two files.

I want to match the 1st column of first file to 1st column of second file. If there is a match I'd like to print the 2st column of second file, if not 0.

I have tried awk (awk 'FNR==NR{a[$1]=$0;next} {print a[$1]}' 2.txt 1.txt) but it is not working.

F1.txt

A

B

C

D

E

F

G

H

I

J

F2.txt

A 0.5

E 1

H 0.5

J 1

Desired_output

A 0.5

B 0

C 0

D 0

E 1

F 0

G 0

H 0.5

I 0

J 1