This is a C Program to implment gerp linux command. Grep is a command-line utility that searches a plain text that matches a regular expression.
Here is source code of the C Program to Implement the Program Used in grep/egrep/fgrep. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<dirent.h>
int main() {
char fn[10], pat[10], temp[200];
FILE *fp;
printf("\n Enter file name : ");
scanf("%s", fn);
printf("Enter the pattern: ");
scanf("%s", pat);
fp = fopen(fn, "r");
while (!feof(fp)) {
fgets(temp, sizeof(fp), fp);
if (strcmp(temp, pat))
printf("%s", temp);
}
fclose(fp);
return 1;
}
Output:
$ gcc grep.c $ ./a.out Enter file name : program.c Enter the pattern: while #include<stdio.h>#include<dirent.h>int main() { char fn[10], pat[10], temp[200]; FILE *fp; printf("\n Enter file name : "); scanf("%s", fn); printf("Enter the pattern: "); scanf("%s", pat); fp = fopen(fn, "r"); while (!feof(fp)) { fgets(temp, sizeof(fp), fp); if (strcmp(temp, pat)) printf("%s", temp); } fclose(fp); return 1;}}
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books