首页 > 程序员, 软件 > 文件grep-选出两个文件中相同/不同的内容

文件grep-选出两个文件中相同/不同的内容

2010年2月2日 发表评论 阅读评论

升级于2010-6-10,发现了一个bug,当时用notmatch模式时,如果文件2先eof将导致文件1中的内容没有输出

这个是最近做数据统计用的比较多的一个脚本,目的是根据key值,输出文件中相同或者不同的行。例如文件A的样子:

10000007^H_O
10000036^Hzerui
10000037^Hyanpeng_haha

文件B的样子:

10000037^Hyanpeng_haha

注意文件中的^H是一个字符,在vim中使用输入ctrl+v再输入ctrl+h就可以看到了,这里用它主要是起到文件不同列之间的分隔符作用

使用命令

php mygrep.php A B

可以得到“10000037^Hyanpeng_haha”这一行数据,使用命令

php mygrep.php A B “notmatch”

可以得到另外两行数据。

脚本默认使用第零列作为筛选依据,默认使用^H作为分隔符,在脚本中自己配置一下可以更改。脚本工作时,要求筛选依据是已排序的。对于排序,可以使用sort命令操作文件,sort通过-t参数指定分隔符,-f指定按照那一列进行排序。

mygrep.php的代码:

$base_file_name    = $argv[1];
$check_file_name   = $argv[2];
$mode              = isset($argv[3])?$argv[3]:false;

$field_index = 0;
$delimiter = ',';

//////////////////////////////////////////////////////////////
$base_file = @fopen($base_file_name, 'r');
$check_file = @fopen($check_file_name, 'r');
$read_base_file = true;
$compare_result = 0;
$readed_base_file_arr = array();

//以要检查的文件作为外层循环
while(!feof($check_file)){
    //读取要检查的文件
    $readed_check_arr = explode($delimiter, trim(fgets($check_file)));
    do{
        //读取基准文件
        if($read_base_file === true){
            $readed_base_arr = explode($delimiter, trim(fgets($base_file)));
        }   

        //作比较
        $compare_result = strcmp($readed_check_arr[$field_index], $readed_base_arr[$field_index]);
        $grep_flag = ($compare_result == 0);
        //如果是要取出不相等的数据
        //且检查key小于或等于基准key时,说明需要将检查key下移一行,即此比较key已经比较完成
        //此时如果比较结果相等则不输出,
        //如果比较结果不等,并且检查key大于基准key则输出,如果检查结果小于基准key,说明检查key要下移继续检查
        if($mode !== false && (strcmp($mode, "notmatch") == 0)
            && $compare_result >= 0){
            $grep_flag = !$grep_flag;
        }

        //print("comparing check=".$readed_check_arr[$field_index]." base=".$readed_base_arr[$field_index]." result=$compare_result
grep_flag=$grep_flag\n");

        if($grep_flag){
            print implode($delimiter, $readed_base_arr)."\n";
        }

        //总是设置为要读取,如果跳出循环,则由外面设置
        $read_base_file = true;

    //如果检查的key是大于基准的key,并且检查文件未到尾,则重新读取一行基准文件进行检查
    }while($compare_result > 0 && !feof($base_file));

    if(feof($base_file)){
        break;
    }

    //当检查key小于基准key时,不读取下个基准key,等于则是因为已经比较过,两个都需要读取
    if($compare_result < 0){
        $read_base_file = false;
    }
}

//当check_file结束了但是base_file未结束
do{
    print implode($delimiter, $readed_base_arr)."\n";
    $readed_base_arr = explode($delimiter, trim(fgets($base_file)));
}while(!feof($base_file));

fclose($check_file);
fclose($base_file);

---------------------------------------------------------------
本站作品根据创作共同协议进行授权, 转载时请务必以超链接形式标明文章原始出处
原文地址:http://www.mirecle.com/2010/02/02/file-grep-select-two-files-same-different-content.html
---------------------------------------------------------------

您可能会喜欢:

  1. php中getopt的缺陷及修复
  2. sql运行简单封装
  3. sigterm sigint sigkill 区别
  4. php反射效果:基类访问子类数据
分类: 程序员, 软件 标签:

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.