Click here to Skip to main content
15,886,094 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
write a c programme to mimic "grep" command which is in linux...and the error is if we search a pattern which is in a file...if that pattern is in last line then it is searching and printing 2 times ...exept last line if we search the pattern which is in another line it is printing exactly that line 1 time

What I have tried:

#include<stdio.h>
#include<string.h>
void main()
{
char fn[30],pat[30],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat)!=NULL)
printf("%s",temp);
}
fclose(fp);
}
Posted
Updated 19-May-20 18:33pm
v3
Comments
Richard MacCutchan 19-May-20 8:51am    
You have allocated 200 characters for temp, but then you call fgets and tell it that temp is 1000 characters long. Use #defines to set array sizes so you use the same value everywhere.
k5054 19-May-20 10:50am    
Better yet, use getline() e.g.
#include <stdio.h>
#include <stdlib.h>

char *input = NULL;
size_t len = 0
ssize_t input_len;

input_len = getline(&input, &len, stdin);
/* ... process input ... */
free(input);
Richard MacCutchan 19-May-20 11:01am    
And I don't think this was meant for me either. Are you having a bad day?
sai charan_12 19-May-20 11:06am    
can i get a exact code for this answer please
sai charan_12 19-May-20 11:07am    
i tried it a lot but i am not able to code it so please ...

Start by documenting yourself about grep command, so that you can make a list of functionalities to replicate:
linux grep[^]

Then document yourself about regular expressions in C:
c regular expressions[^]

When you have accomplished former tasks, then you can start thinking about how you are going to implement it.
 
Share this answer
 
Comments
sai charan_12 19-May-20 7:48am    
actually i am getting the output a small error can u help me
phil.o 19-May-20 7:51am    
No, because you did not provide any information about what you have tried, nor what this error is.
sai charan_12 19-May-20 7:58am    
sir i have given the code what i tried and i have mentioned the error in the programme now sir
Start by reading what grep does: The Grep Command Tutorial With Examples For Beginners - OSTechNix[^] should be a start, but it's a complicated program, and once you've handled that, you will probably want to work with most of these as well: grep tutorial - Google Search[^]

Once you fully understand it, you can start to work one the "heart" of grep - the Regular Expression processor.
C doesn't have one of those built in, so you'll probably need to write your own. More reading will be required: Let’s Build a Regex Engine | Alexander Grebenyuk[^]

Do not expect this to be a "quick project" - I'd be looking at weeks to get it right myself, so a relative beginner should plane for a considerable amount more time!

Good luck!
 
Share this answer
 
Comments
sai charan_12 19-May-20 7:53am    
sir i already what grep does in linux ..it searches the required pattern from file and prints the whole line in which pattern contains...i am getting a small error in output..only if we search a pattern which is in last line that is printing 2 times....can u help what is the wrong in the programme..is the programme which i wrote is corect ? i will be greatful if u help me sir
OriginalGriff 19-May-20 8:12am    
Use the debugger and work out what is going on.

But if you think that strstr does what a Regular Expression does, you are in for a shock when you learn the truth! The strstr function is a very basic character matching function, not in any way like a Regular Expression processor!
Here's how I would read that file :
C++
#include<stdio.h>
#include<string.h>

#define BUFFER_LENGTH = 511;

int main( int ac, char * av[] )
{
    char lineBuffer[BUFFER_LENGTH+1];
    FILE *fp = NULL;
    int count = 0;

    if( ac < 3 )
    {
        fprintf( stderr, "A string pattern and a file name are required\n" );
        return 1;
    }

    fp = fopen( av[2], "r" );
    if( ! fp )
    {
        fprintf( stderr, "Error - unable to open %s\n", av[2] );
        return 2;
    }

    while( fgets( lineBuffer, BUFFER_LENGTH, fp ) )
    {
        if( strstr( lineBuffer, av[1] ) )
        {
            printf( "%s", lineBuffer );
            ++count;
        }
    }
    fclose( fp );
    printf( "found %d occurrences\n", count );
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900