繁体   English   中英

如何比较两个文件,一个有不同的字段,另一个是 ac 文件,使用 shell 脚本?

[英]How to compare two files, one has different fields and other is a c file, using shell script?

我有以下两个文件:

文件1:

#include<stdio.h>   printf,scanf
#include<string.h>  strcpy,strcat,strlen
#include<time.h>    time

文件2.c:

int main {
    char str1[20] = "BeginnersBook";
    printf("Length of string str1: %d", strlen(str1));
    return 0;
}

我们必须在file2中搜索file1(field2)的功能。 如果它们存在,那么我们应该将结果写入一个名为 output.txt 的不同文件中。 它应该只包含文件名,适当的头文件应该在那里

File2:headerfile1,headerfile2

这可能与grepawk吗?

您可以将 file1 转换为模式列表(每行一个)并将其用作参数

grep -f patterns file2

使用以下脚本

#!/bin/sh
HEAD=
while read p; do
    i=1;
    for word in $p;do
        if [ "$i" = "1" ];then
            HEAD=$word
            echo "header = $HEAD"
        else
            FOUND=0
            for j in $(echo "$word" | tr "," "\n");do
                if grep -Fq "$j" file2.c
                then
                    FOUND=1
                fi
                if ["$FOUND" = "1" ];then
                    #write it into your c file; I am skipping that code
                fi
            done
        fi
        i=0
    done
done < file1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM