: how to simulate the grep -l option of unix in c. : where the command line argument passed as below: : grep -l [pattern] [file name(where to be searched)] :
compile this code and run as follows
prog_name word_to_be_found list of files to search in
notes: - search is case sensitive - does not do recursive search (you can easily add this useing either the ftw() or nftw() functions to walk a directory tree) - does not accept regular expression (look into glibc regular expressions or the perl compatible regular expression (PCRE) libraries f you want to add this) - loads entire file into memory, so be careful not to search large files
Comments
: where the command line argument passed as below:
: grep -l [pattern] [file name(where to be searched)]
:
compile this code and run as follows
prog_name word_to_be_found list of files to search in
notes:
- search is case sensitive
- does not do recursive search (you can easily add this useing either the ftw() or nftw() functions to walk a directory tree)
- does not accept regular expression (look into glibc regular expressions or the perl compatible regular expression (PCRE) libraries f you want to add this)
- loads entire file into memory, so be careful not to search large files
[code]
#include
#include
#include
#include
/* calculate filesize */
int calc_bufsize(char *filename)
{
struct stat st;
stat(filename, &st);
return ((int)st.st_size);
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Usage:
%s ...
", argv[0]);
return -1;
}
FILE *fp;
char *filename;
int x = 2;
/* process each file */
for(x; x != argc; x++)
{
filename = argv[x];
if( (fp = fopen(filename, "r")) == NULL)
{
printf("Failed to open file: %s
", argv[2]);
return -2;
}
int BUFSIZE = calc_bufsize(filename);
/* read ENTIRE file into buf[] */
char buf[BUFSIZE];
fread(&buf, sizeof(char), BUFSIZE, fp);
/* search buf for word (case sensitive) */
char *ans = strstr(buf, argv[1]);
/* word found, print filename */
if(ans != NULL)
printf("%s
", filename);
/* word not found, do nothing */
fclose(fp);
}
return 0;
}
[/code]
------
nugent
#include
int main(int argc, char *argv[])
{
FILE *fp;
/*without option*/
if(argc==3)
{
printf("
Lines with the search string
");
fp=fopen(argv[2],"r");
char search[100],string[100];
strcpy(search,argv[1]);
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
if(strstr(string,search)!=NULL)
printf("%s",string);
}
fclose(fp);
}
/*options included*/
else if(argc==4)
{
/*option -n included*/
if(argv[1][1]=='n')
{
printf("
Line Numbering
");
fp=fopen(argv[3],"r");
char search[100],string[100];
int i=1;
strcpy(search,argv[2]);
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
if(strstr(string,search)!=NULL)
printf("%d --> %s",i,string);
i++;
}
fclose(fp);
}
/*option -v included*/
else if(argv[1][1]=='v')
{
printf("
Lines without the search string
");
fp=fopen(argv[3],"r");
char search[100],string[100];
strcpy(search,argv[2]);
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
if(strstr(string,search)==NULL)
printf("%s",string);
}
printf("
");
fclose(fp);
}
/*option -c included*/
else if(argv[1][1]=='c')
{
printf("
Number of lines with the search string
");
fp=fopen(argv[3],"r");
char search[100],string[100];
int i=0;
strcpy(search,argv[2]);
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
if(strstr(string,search)!=NULL)
i++;
}
printf("%d
",i);
fclose(fp);
}
/*option -x included*/
else if(argv[1][1]=='x')
{
printf("
Lines with the search string alone
");
fp=fopen(argv[3],"r");
char search[100],string[100];
strcpy(search,argv[2]);
search[strlen(search)]='
';
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
if(strcmp(search,string)==0)
printf("%s",string);
}
fclose(fp);
}
/*option -i included*/
else if(argv[1][1]=='i')
{
printf("
Lines with the search string - case insensitive
");
fp=fopen(argv[3],"r");
char search[100],string[100],search_lwr[100],string_lwr[100];
int j;
strcpy(search,argv[2]);
for(j=0;search[j];j++)
search_lwr[j]=tolower(search[j]);
search_lwr[strlen(search)]='';
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
for(j=0;string[j];j++)
string_lwr[j]=tolower(string[j]);
string_lwr[strlen(string)]='';
if(strstr(string_lwr,search_lwr)!=NULL)
printf("%s",string_lwr);
}
fclose(fp);
}
}
else
printf("
Error in input");
}
and another one is
Code
/* Program for grep command with options