0

There are two files I am going to extract the data:

file1.txt:

Type Serial ID Element Hit_Possibility
Yasuo 19-2 19623 Hasaki 

file2.csv:

Date,Name,Order,Hit Possibility
12-Aug,Ken,1,256
12-Aug,Tom,19,498
12-Aug,Ray,36,753

How can I combine two files into a text file with the following results:

Type Serial ID Element Hit_Possibility
Yasuo 19-2 19623 Hasaki 498

Here is what I have tried before:

awk -F "\"*,\"*" 'NR==1{print $0;next} NR==FNR{Arr[$2]=$NF;next}{split($2,b,"-");if(b[1] in Arr){print $0,Arr[b[1]]}}' file2.csv file1.txt

I think I cannot get the results because of the NR==FNR part. How can I get the result file I want?

1
  • What is the key to combine data from these two files? Commented Oct 11, 2018 at 8:58

2 Answers 2

3

You can try this awk:

awk -F, '
   (NR==FNR)&&(NR>1){a[$3]=$4;next}  # Get keys of file2 into array a
   ($2 in a){$0=$0 a[$2]}            # If key in file1 is part of the array a, append value
   NR!=1                             # Print all lines of file1 and file2 except the header
' file2 FS='[ -]' file1
1
  • clever change of FS between file !! I dunno you can do that ! Commented Oct 11, 2018 at 9:02
0

You could simplfy oliv's answer a bit:

awk -F, '
NR==FNR     {a[$3] = $4
             next}
            {print $0, a[$2]
            }
' file2 FS='[ -]' file1
Type Serial ID Element Hit_Possibility 
Yasuo 19-2 19623 Hasaki  498

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.