我用awk没什么问题,因为它会让我白发苍苍..
我有两个内容不同的文件,但第一列的值是相同的.
文件在脚本期间生成为CSV文件(以分号分隔)和如下所示的锁定:
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
我想比较这两个文件,并创建第三个文件,该文件包含"main_file.csv"中的行,该行与"Filter_file.csv"中的行匹配,使用第一个列值作为比较.
如示例所示,我应该得到一个包含一行的"ResultFile.csv",但不幸的是,我得到的是一个空文件.
专家输出应为:
151622-0;151622;0;0;10;;;programmed
这是我试过的命令:
awk 'BEGIN {FS=OFS=";"} NR==FNR{a[$1]=1; next} a[$1]{print}' filter_file.csv main_file.csv > result_file.csv
如果我理解正确的话,它应该是这样解释的:
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
但我恐怕错过了一些东西:/
编辑:更新以添加一些上下文:
该命令是从如下所示的php脚本执行的:
$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);
其中,$FILTER_FILE、$REF_FILE和$MATCH_FILE是文件的完整路径.
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