Bash file 1 and file then add field

Hi All,

Seeking for your assistance regarding on how to bash file 1 and file 2 then add the field if the bash is true.

Ex.

nik1.csv - input file 1

1,0054,0001,4,5,6,7,May 1,USA 
11,0053,0002,14,15,16,17,May 2,PHL 
40,0052,0003,43,44,45,46,May 5,GER 

nik2.csv - input file 2

20,21,22,23,24,0054,0001,May 5 
30,31,32,33,34,0053,0002,May 7 

Input File 2 Expected Output:
20,21,22,23,24,0054,0001,May 5,USA - Added the 8th Field(USA) because the the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1.
30,31,32,33,34,0053,0002,May 7, PHL - Added the 8th Field(PHL) because the the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1.

If the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1, i'm going to add the 8th field of input file 1 into input file 2.

I don't know how to code it. T_T

Please advise,

Thanks,
-nik

---------- Post updated at 11:52 AM ---------- Previous update was at 11:03 AM ----------

Seeking on assistance pls..

Thanks,
-nik

Try :

$ awk -F, 'FNR==NR{A[$6]=$0;next}($6 in A){$0=A[$6] FS $8}A[$6]'  inputfile2 inputfile1
20,21,22,23,24,0054,May 5,USA
30,31,32,33,34,0053,May 7 ,PHL

OR

$ awk -F, 'FNR==NR{A[$6]=$8;next}($6 in A){$0=$0 FS A[$6]}A[$6]'  inputfile1 inputfile2
20,21,22,23,24,0054,May 5,USA
30,31,32,33,34,0053,May 7 ,PHL
1 Like

okay Sir Akshay Hegde i will try and keep you posted.

Thanks,
-nik

---------- Post updated at 12:24 PM ---------- Previous update was at 12:20 PM ----------

Hi Sir Akshay,

May i know where in this code is the 6th field of file 1?

$ awk -F, 'FNR==NR{A[$6]=$0;next}($6 in A){$0=A[$6] FS $8}A[$6]'  inputfile2 inputfile1
$ awk -F, 'FNR==NR{A[$6]=$8;next}($6 in A){$0=$0 FS A[$6]}A[$6]'  inputfile1 inputfile2

Please advise,

Thanks,
-nik

Red color represents 6th column of inputfile1

1 Like

Thank you very much Sir Akshay Hegde.

How about If the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1, i'm going to add the 8th field of input file 1 into input file 2.

Please see edited scenario above.

Thanks,
-nik

awk -F"," 'NR==FNR{a[$2"|"$3]=$9; next}
a[$6"|"$7]{ print $0 FS a[$6"|"$7]}' file1 file2
1 Like
awk ' FNR==NR{A[$2 FS $3]=$8;next}($6 FS $7 in A){$0 = $0 FS A[$6 FS $7]}A[$6 FS $8]' file1 file2

FNR==NR{A[$2 FS $3]=$8;next}
Array A with index $2 FS $3 fields of file1 holds $8 field of file1

($6 FS $7 in A){$0 = $0 FS A[$6 FS $7]}
if $6 FS $7 fields (index) found in Array A then line = line + field Separator + $8 field of file1 which is in Array A with index $6 FS $7 of file2

...}A[$6 FS $7] ' file1 fil2

if A[$6 FS $7] is not empty then true and it prints line
if A[$6 FS $7] is empty then it false it will not print

You might aware
1 --> true
0 --> false

1 Like

Thank you very much Sir pravin27. I will try it now.

Thank you very much Sir Akshay Hegde.

Hi,

Sounds good , It would be great if you can use "Thanks" button (right side) instead of post to say thanks.

~ Pravin ~

1 Like

Thanks All for your help. It's now working.

Cheers,
-nik

---------- Post updated at 02:49 PM ---------- Previous update was at 02:48 PM ----------

Yup. i already hit Thanks button. ^^

Cheers,
-nik

If you keep on editing the input, the answer we post looks stupid, and other who refer or comes here to answer feel that the solution which we given has no relationship with given input, and answer we posted is nonsense. Please note threads are not only referred by you, there are many people who refers this forum, I had noticed that you edited input in #1 . Please don't do like this.

1 Like

Oh i see My Apology Sir Akshay Hegde i will not do it again.

Apology,

Thanks,
-nik