#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
FILE *fp;
/*without option*/
if(argc==3)
{
printf("\nLines with the search string\n");
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("\nLine Numbering\n");
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("\nLines without the search string\n");
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("\n");
fclose(fp);
}
/*option -c included*/
else if(argv[1][1]=='c')
{
printf("\nNumber of lines with the search string\n");
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\n",i);
fclose(fp);
}
/*option -x included*/
else if(argv[1][1]=='x')
{
printf("\nLines with the search string alone\n");
fp=fopen(argv[3],"r");
char search[100],string[100];
strcpy(search,argv[2]);
search[strlen(search)]='\n';
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("\nLines with the search string - case insensitive\n");
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)]='\0';
while(!feof(fp))
{
fgets(string,sizeof(string),fp);
for(j=0;string[j];j++)
string_lwr[j]=tolower(string[j]);
string_lwr[strlen(string)]='\0';
if(strstr(string_lwr,search_lwr)!=NULL)
printf("%s",string_lwr);
}
fclose(fp);
}
}
else
printf("\nError in input");
}
and another one is
Code
/* Program for grep command with options –n, -i, -c, -v, -x for one/more files
* -n --- Precedes each line with the relative line number in the file.
* -i --- Ignores the case (uppercase or lowercase) of letters when making comparisons.
* -c --- Displays only a count of matching lines.
* -v --- Displays all lines not matching the specified pattern.
* -x --- Displays lines that match the specified pattern exactly with no additional
Characters
*/ .
/* Header files inclusion */
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 50
int option = 0; /*variable declaration */
int no_of_files = 0;
/* This function sets the value for the global variable ‘option’ and this value is fixed
* based on the options given with grep command. The values used are,
* -n --- 1
* -i --- 2
* -c --- 3
* -v --- 4
* -x --- 5
*/
void set_option(char c)
{
switch(c)
{
case 'n':
option = 1;
break;
case 'i':
option = 2;
break;
case 'c':
option = 3;
break;
case 'v':
option = 4;
break;
case 'x':
option = 5;
break;
default:
printf("\ngrep: illegal option -- %c\n",c);
printf("Usage: grep -nicvx pattern file . . .\n");
break;
}
}
/* The function implements Naïve string matching algorithm */
int search_string(char *line, char *pattern)
{
int n = strlen(line);
int m = strlen(pattern);
int i,j;
for(i=0;i<n-m;i++)
{
j=0;
while(j<m && line[i+j]==pattern[j])
{
j++;
}
if(j==m)
return i;
}
return -1;
}
/* The function reads each line from the file(s) specified and process it based on the
* option and it calls search_string() function for pattern matching
*/
void file_search(char *file_name, char *pattern,int temp_option)
{
FILE *fp;
char line[SIZE];
char temp_line[SIZE];
char *used_line;
int i;
int line_no = 0;
int count = 0;
if((fp = fopen(file_name,"r"))==NULL)
{
printf("Can't open %s\n",file_name);
exit(0);
}
while(fgets(line,SIZE,fp))
{
if(option == 5)
line[strlen(line)-1]='\0';
used_line =line;
line_no++;
switch(option)
{
case 0:
if((search_string(used_line,pattern))!=-1)
{
if(no_of_files == 1)
printf("%s",line);
else
printf("%s:%s",file_name,line);
}
break;
case 1:
if((search_string(used_line,pattern))!=-1)
{
if(no_of_files == 1)
printf("%d:%s",line_no,line);
else
printf("%s:%d:%s",file_name,line_no,line);
}
break;
case 2:
for(i=0;pattern[i]!=NULL;i++)
pattern[i] = tolower(pattern[i]);
pattern[i] = NULL;
for(i=0;line[i]!=NULL;i++)
temp_line[i] = tolower(line[i]);
temp_line[i] = NULL;
used_line = temp_line;
if((search_string(used_line,pattern))!=-1)
{
if(no_of_files == 1)
printf("%s",line);
else
printf("%s:%s",file_name,line);
}
break;
case 3:
used_line = line;
if((search_string(used_line,pattern))!=-1)
count++;
break;
case 4:
if((search_string(used_line,pattern))== -1)
{
if(no_of_files == 1)
printf("%s",line);
else
printf("%s:%s",file_name,line);
}
break;
case 5:
if(strlen(line)==strlen(pattern))
{
if((search_string(used_line,pattern))== 0)
{
if(no_of_files == 1)
printf("%s",line);
else
printf("%s:%s",file_name,line);
}
}
break;
}
}
if(option == 3)
{
if(no_of_files == 1)
printf("%d\n",count);
else
printf("%s:%d\n",file_name,count);
}
}
/* main function */
int main(int argc,char*argv[])
{
char *search_string;
char *file_name;
int index = 1;
int i = 0;
if(argc<2)
{
printf("Too low arguments\n");
exit(0);
}
if(argv[index][0]== '-')
{
set_option(argv[index][1]);
++index;
no_of_files = argc - 3;
}
else
no_of_files = argc - 2;
search_string = argv[index];
if(option == 5)
{
strcat(search_string,"\n");
}
if((strlen(search_string)) == 0)
{
printf("No remembered search string\n");
exit(0);
}
for(i=0;argv[++index]!= NULL;i++)
{
file_name = argv[index];
file_search(file_name,search_string,option);
}
return 0;
}
another one is
#include<stdio.h>
#include<string.h>
int main(int argc,char *argv[])
{
int i,j;
char a1[100],b1[100];
FILE *fp;
if(argc==3)
{
fp=fopen(argv[2],"r");
strcpy(a1,argv[1]);
i=0;
while(!feof(fp))
{
fgets(b1,sizeof(b1),fp);
if(strstr(b1,a1)!=NULL)
{
i++;
printf("%s",b1);
}
}
if(i==0)
printf("\n NO MATCH FOUND \n");
fclose(fp);
}
else if(argc==4)
{
int i=0;
char a[100],b[100],c[100];
fp=fopen(argv[3],"r");
strcpy(a,argv[2]);
if(argv[1][1]=='n')
{
j=0;
printf("Lno Line\n");
while(!feof(fp))
{
fgets(b,sizeof(b),fp);
i++;
if(strstr(b,a)!=NULL)
{
j++;
printf("%d %s\n",i,b);
}
}
if(j==0)
printf("\nNO MATCH FOUND\n");
fclose(fp);
}
if(argv[1][1]=='c')
{
i=0;
j=0;
fp=fopen(argv[3],"r");
while(!feof(fp))
{
fgets(b,sizeof(b),fp);
if(strstr(b,a)!=NULL)
{
i++;
}
}
printf("\n Number of lines containing Pattern %s is : %d\n",a,i);
if(i==0)
printf("NO MATCH FOUND\n");
fclose(fp);
}
if(argv[1][1]=='v')
{
i=0;
fp=fopen(argv[3],"r");
while(!feof(fp))
{
fgets(b,sizeof(b),fp);
if(strstr(b,a)==NULL)
{
i++;
printf("%s",b);
}
}
if(i==0)
printf("\nALL LINES CONTAINS THE PATTERN\n");
fclose(fp);
}
if(argv[1][1]=='x')
{
fp=fopen(argv[3],"r");
a[strlen(a)]='\n';
i=0;
while(!feof(fp))
{
fgets(b,sizeof(b),fp);
if(strcmp(a,b)==0)
{
i++;
printf("%s",b);
}
}
if(i==0)
printf("\nNO LINE MATCHES THE PATTERN\n");
fclose(fp);
}
if(argv[1][1]=='i')
{
j=0;
fp=fopen(argv[3],"r");
for(i=0;a[i];i++)
a[i]=tolower(a[i]);
while(!feof(fp))
{
fgets(b,sizeof(b),fp);
for(i=0;b[i];i++)
c[i]=tolower(b[i]);
if(strstr(c,a)!=NULL)
{
printf("%s",b);
i++;
}
}
if(j==0)
printf("\nNO MATCH FOUND\n");
fclose(fp);
}
}
else
{
printf("\nEnter Arguments As Per Grep Command\n ");
}
return 0;
}