#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
char *buff,*t1,*t2,*t3,*t4,ch;
FILE *fp;
main()
{
   int n,i=0,count=0;
   clrscr();
   while(1)
   {
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
t4=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
n=sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);
if(strcmp(t1,"exit")==0)
exit(0);

if(strcmp(t1,"grep")==0)
{

 if((fp=fopen(t4,"r"))==NULL)
printf("File not found\n");
 else if(strcmp(t2,"-v")==0)
  {
while(fgets(buff,80,fp))
{
if((strstr(buff,t3))==NULL)
{
printf("%s\n",buff);

}

}
}
else if(strcmp(t2,"-c")==0)
{
count=0;
while(fgets(buff,80,fp))
{
if((strstr(buff,t3))!=NULL)
{
//p=buff;
//while((p=strstr(p,t3))!=NULL)
//{
count++;

//}
}
}
printf("No of occurences of %s= %d\n",t3,count);
}
else if(strcmp(t2,"-n")==0)
{
i=0;
while(fgets(buff,80,fp))
{
if((strstr(buff,t3))!=NULL)
{
printf("%d: %s\n",i,buff);

}

i++;
}

}
else
 printf("Command not found\n");
 }
 fclose(fp);
   }



}

Post a Comment

Previous Post Next Post