读取file1中的行,如果file2中不存在该行,则将此行写入新文件。
通过散列字符串的长度进行比较。
file1中的哈希值:
cf03189f5b05eb1a9658f80d7a0e9f02:_#.g}
edbe6de8b3ee19b45e092147f57af7b8:]mNon
47253940f843f258ffd265d13f365d70:/u'yv
5701aa8e2aa7e1cfd16ca4076bd1732a:@AQ1z
b3c0866e6fd56776bc4a18d3c87cc725:t$5OV
7a1e6090568e076c55df9dc7abf356c6:9rC@p
04046da33706518d9b15a38bcddb448e:!DFPk
file2中的哈希值:
edbe6de8b3ee19b45e092147f57af7b8:]mNon:str1
47253940f843f258ffd265d13f365d70:/u'yv:2str
b3c0866e6fd56776bc4a18d3c87cc725:t$5OV:3str1ng
这是一个用 C 编写的工作示例:
#include <stdio.h>
#include <string.h>
#define LINE_LENGTH 80
typedef enum {FALSE=0,TRUE=1} BOOL;
int main(int argc, char *argv[])
{
FILE *fpin1 = NULL;
FILE *fpin2 = NULL;
FILE *fpout = NULL;
char line [LINE_LENGTH]={0};
char line1[LINE_LENGTH]={0};
BOOL bCheck = FALSE;
size_t ncHash = 0;
size_t count = 0;
if (argc != 4)
{
printf("Usage:%s <file1> <file2> <OutFile>\n", argv[0]);
return 1;
}
/*
Opening input files (file1 and file2) for reading in text mode.
The output file (OutFile) is open for writing.
*/
if(((fpin1=fopen(argv[1],"r"))==NULL) ||
((fpin2=fopen(argv[2],"r"))==NULL) ||
((fpout=fopen(argv[3],"w"))==NULL))
{
printf("Error! Could not open files\n");
return 1;
}
while(fgets(line, sizeof(line), fpin1)!=NULL) /* Read hash line from the first file1 */
{
bCheck=FALSE;
while(fgets(line1, sizeof(line1), fpin2)!=NULL) /* Read hash line from the second file2 */
{
if(!strncmp(line, line1, 38)) /* Compares 38 characters of the line in file1 to those of the file2 */
{
bCheck=!bCheck;
break;
}
}
if(!bCheck) /* Does compared line are the same ? */
{
fputs(line,fpout); /* Yes - write them in a file OutFile */
ncHash++; /* Count identical lines */
}
rewind(fpin2); /* Seek to the beginning of the file2 */
count++; /* Counting the read lines in file1 */
}
printf("\nDone...\n");
fclose( fpin1);
fclose( fpin2);
fclose( fpout);
return 0;
}
该程序的OutFile是:
cf03189f5b05eb1a9658f80d7a0e9f02:_#.g}
5701aa8e2aa7e1cfd16ca4076bd1732a:@AQ1z
7a1e6090568e076c55df9dc7abf356c6:9rC@p
04046da33706518d9b15a38bcddb448e:!DFPk
- 我想知道怎么写
awk
?