C/C++ on Linux/Unix

Moderators: Lundin
Number of threads: 314
Number of posts: 633

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
grep command simulation Posted by phulguni on 1 Mar 2006 at 4:52 PM
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)]
Report
Re: grep command simulation Posted by nugent on 15 Jun 2006 at 11:29 AM
: 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


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/* 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:\n\t%s <word> <files> ...\n", 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\n", 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\n", filename);

                /* word not found, do nothing */

                fclose(fp);
        }
        return 0;
}


------
nugent



Report
Re: grep command simulation Posted by anandkumarrs6 on 12 Jan 2010 at 8:08 PM
Can you help me with a sample output for this program ....

Report
Re: grep command simulation Posted by ganeshcece on 30 Aug 2011 at 4:22 AM
#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;
}






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.