Simulate grep Write a MyGrep utility based on C. This MyGrep utility is similar
ID: 3706599 • Letter: S
Question
Simulate grep
Write a MyGrep utility based on C. This MyGrep utility is similar to grep utility provided by Unix. Take four features below to finish.
MyGrep utility takes some options, words and text file as arguments.
•$MyGrep –c “This is a list of words” test.txt
Count the occurrences of string “This is a list of words” in the content of file test.txt
•$MyGrep –c -i “This is a listof words” test.txt
Count the occurrences of string “This is a list of words” in the content of file test.txt and ignore cases
•$MyGrep –o “This is a list of words” test.txt
Output all lines containing “This is a list of words” and highlight the matched string
•$MyGrep –s test.txt
Remove all leading spaces in each line and output the result.
•$MyGrep –n test.txt
In the output, add a line number at the begging of each line.
•$MyGrep –n “This is a list of words” test.txt
Output the lines containing string “This is a list of words” and add a line number at the begging of each matched line.
•$MyGrep –k “This” “is” “a” “list” “of” “words” test.txt
Count the occurrences for each word and output the words along with their occurrences in a decreasing order.
For example, the output could be
a 20
is 8
list 5
words 3
of 2
this 1
Explanation / Answer
//I have listed down the different program to simulate the workign of grep program whcih can be clubbed into one as //required this will help in simulating the grep utility in c
1. For matching the pattern from a file:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<string.h>
#include <fcntl.h>
void match_pattern(char *argv[])
{
int fd,r,j=0;
char temp,line[100];
if((fd=open(argv[2],O_RDONLY)) != -1)
{
while((r=read(fd,&temp,sizeof(char)))!= 0)
{
if(temp!=' ')
{
line[j]=temp;
j++;
}
else
{
if(strstr(line,argv[1])!=NULL)
printf("%s ",line);
memset(line,0,sizeof(line));
j=0;
}
}
}
}
main(int argc,char *argv[])
{
struct stat stt;
if(argc==3)
{
if(stat(argv[2],&stt)==0)
match_pattern(argv);
else
{
perror("stat()");
exit(1);
}
}
}
2. For matching a line
//Line matching
#include<stdio.h>
#include<string.h>
#include <errno.h>
int main(int argc, char *argv[]) {
FILE *fp;
char fline[100];
char *newline;
int i, count = 0, occ = 0;
// This is needed to ensure that the arguments are passed correctly:
if (argc < 3) {
printf("Not enough command line parameters given! ");
return 3;
}
fp = fopen(argv[1], "r");
// fopen will return if something goes wrong. In that case errno will
// contain the error code describing the problem.
if (fp == NULL) {
printf("File could not be opened, errno is %d ",errno);
return 3;
}
while (fgets(fline, 100, fp) != NULL) {
count++;
if (newline = strchr(fline, ' '))
*newline = '';
if (strstr(fline, argv[2]) != NULL) {
// Each line found can be printed by using the following printf command
printf("%s %d %s ", argv[1], count, fline);
occ++;
}
}
printf(" Occurence= %d", occ);
return 1;
}
3. To count no of occurance of word i have give a function whcih can be used to achieve the same:
int countOccurrences(char * str, char * toSearch)
{
int i, j, found, count;
int stringLen, searchLen;
stringLen = strlen(str); // length of string
searchLen = strlen(toSearch); // length of word to be searched
count = 0;
for(i=0; i <= stringLen-searchLen; i++)
{
/* Match word with string */
found = 1;
for(j=0; j<searchLen; j++)
{
if(str[i + j] != toSearch[j])
{
found = 0;
break;
}
}
if(found == 1)
{
count++;
}
}
return count;
}
4. Remove leading as well as trailing space from string:
5. Count no of occurances of word in a file, program doseen't print the the words in decreasing order but can be helpful to device a logic: