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
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Apply for C Internship
- Buy C Books
- Buy Computer Science Books