I have little issue using awk which start to give me white hairs..
I have two files with different contents, but the first column value is the same.
Files are generated during a script as csv file (semicolon separated) and lokks like so:
main_file.csv
---
151597-21;151597;21;3;15;;"Vente OK";excluded
151598-21;151598;21;3;15;;"Vente OK";excluded
151599-0;151599;0;0;10;;;programmed
151600-0;151600;0;0;10;;;programmed
151601-0;151601;0;0;10;;;programmed
151602-0;151602;0;0;10;;;programmed
151603-0;151603;0;0;10;;;programmed
151604-0;151604;0;0;10;;;programmed
151605-0;151605;0;0;10;;;programmed
151606-0;151606;0;0;10;;;programmed
151607-0;151607;0;0;10;;;programmed
...
151622-0;151622;0;0;10;;;programmed
151623-0;151623;0;0;10;;;programmed
151624-0;151624;0;0;10;;;programmed
151625-0;151625;0;0;10;;;programmed
...
filter_file.csv
---
151622-0;151622;0
I want to compare those two files and create a third one that containts lines from "main_file.csv" that matches the ones in the "filter_file.csv" using the first column value as comparaison.
As the exemple shows, I should get a "result_file.csv" with one line in it, unfortunately I get an empty file.
The experted output should be:
151622-0;151622;0;0;10;;;programmed
Here is the commmand I tried:
awk 'BEGIN {FS=OFS=";"} NR==FNR{a[$1]=1; next} a[$1]{print}' filter_file.csv main_file.csv > result_file.csv
If I'm understood awk correctly, it should explain as this:
awk ' # starting awk program
BEGIN {FS=OFS=";"} # define column separator as commat for both files (main & filter)
NR==FNR{a[$1]=1; next} # during read of the first file (filter_file.csv), create an array 'a' with first column value as index
a[$1]{print} # during read of the second file (main_file.csv), if first column value exist as an index of the array 'a', print the whole line in the 'result_file.csv'
'
filter_file.csv main_file.csv # files to be compared
> result_file.csv # direct the output to the third file
but I'm afraid I missed something :/
EDIT: update to add a bit of context:
the command is executed from a php script like so:
$awk_cmd = 'awk \'BEGIN {FS=OFS=";"} NR==FNR {a[$1]=1; next} $1 in a {print}\' ' . $filter_file . ' ' . $ref_file . ' > ' . $match_file;
exec($awk_cmd);
where $filter_file, $ref_file and $match_file are the full path of the files.
EDIT2 :
I tested the grep command and get the following output:
0000000 1 5 1 6 2 2 - 0 ; 1 5 1 6 2 2 ;
0000020 0 ; 0 ; 1 0 ; ; ; p r o g r a m
0000040 m e d \r \n
0000045