'copy a column from file1 when the ID's matches to file2 and print output according to file 2
I have 2 files,
head file1
ESC_KB2908AA_AS 1 Eco-19-00825
ESC_KB2909AA_AS 2 Eco-20-00081
ESC_KB2910AA_AS 3 Eco-20-00128
ESC_KB2911AA_AS 4 Eco-19-00991
and head file2
ESC_KB2908AA_AS
ESC_KB2908AA_AS
ESC_KB2908AA_AS
ESC_KB2909AA_AS
ESC_KB2909AA_AS
ESC_KB2909AA_AS
ESC_KB2910AA_AS
ESC_KB2910AA_AS
ESC_KB2911AA_AS
ESC_KB2911AA_AS
I want to check if the first column of file1 matches to the first column of file2 and if ID matched then print the output according to file2.
Expected file output :
ESC_KB2908AA_AS 1 Eco-19-00825
ESC_KB2908AA_AS 1 Eco-19-00825
ESC_KB2908AA_AS 1 Eco-19-00825
ESC_KB2909AA_AS 2 Eco-20-00081
ESC_KB2909AA_AS 2 Eco-20-00081
ESC_KB2909AA_AS 2 Eco-20-00081
ESC_KB2910AA_AS 3 Eco-20-00128
ESC_KB2910AA_AS 3 Eco-20-00128
ESC_KB2911AA_AS 4 Eco-19-00991
ESC_KB2911AA_AS 4 Eco-19-00991
I tried the following code but I am getting output only for last line of file2. Could you please help what's wrong with script?
awk 'FNR==NR{key[$1]=$2;next} {$2=key[$1]}1' file1.txt file2.txt > output.txt
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
