Compare two files with awk

Hello,

I have a script which extracts the values from a csv file when a specific date is entered :

#!/bin/sh
awk 'BEGIN{printf("Entrez la date : "); getline date < "-"}
$0 ~ date {f=1;print;next}
/^[0-9]{2}\//{f=0}
f' file1.csv

This script gives me a number of lines with different values.

I have another csv file which contains possible matching values with the first file and with the values below :

Action1;Action11;Action12;Action13
Action2;Action21
Action3;Action31;Action32

How to redirect the matching results of file1 and file2 in another output file ?

Is it possible to do it in the same script ?

Thanx a lot for your help :slight_smile:

Post the content of both files and the desired output based on those files.

Please use code tags!

Sorry :

File 1:

01/12/2010;;;;;;;;;;;
;Action_1;; Comment;Comment;;;;;;"File Comment";
;Action_2;; Comment;Comment;;;;;;"Exclusif";
;Action_3;; Comment;Comment;;;;;;"Not exclusif";
;Action_4;; Comment;Comment;;;;;;"Another File Comment";
;Action_5;; Comment;Comment;;;;;;"File Comment";
;Action_6;; Comment;Comment;;;;;;"File Comment";
02/12/2010;;;;;;;;;;;
;Action_1;; Comment;Comment;;;;;;"File Comment";
;Action_2;; Comment;Comment;;;;;;"Exclusif";
;Action_3;; Comment;Comment;;;;;;"Not exclusif";
;Action_4;; Comment;Comment;;;;;;"Another File Comment";
;Action_5;; Comment;Comment;;;;;;"File Comment";
;Action_6;; Comment;Comment;;;;;;"File Comment";

File 2 :

BP_101;BP_10101;BP_10102;BP_10103
BP_209;BP_20903
BP_0RT;BP_WG7SH

Thanks in advance

---------- Post updated at 08:55 AM ---------- Previous update was at 08:52 AM ----------

Sorry again,

File 2 contains this :

Action_1;Action11;Action12;Action13
Action_2;Action21
Action_3;Action31;Action32
#!/bin/sh
awk 'BEGIN{FS=OFS=";";printf("Entrez la date : "); getline date < "-"}
        FNR==NR {for(i=1;i<=NF;i++)f2[$i];next}
        $0 ~ date {f=1;print;next}
       /^[0-9]{2}\//{f=0}
       f && $2 in f2' file2.csv file1.csv

Thanks a lot for all your help. This forum is really good and very helpful.

Can you please tell me what is the signification of all this :

FNR==NR {for(i=1;i<=NF;i++)f2[$i];next}

I know FNR stands for the ordinal number of the current record in the current file and that NR stands for the ordinal number of the current record from the start of input, but I don't understand the whole sentence

The condition FNR==NR will be true only for the FIRST file passed to awk for processing. Therefore, we read file2 into hash FIRST - then proceed with the processing for the other/file1 files.

2 Likes

Thanks a lot :slight_smile: :b: