C Program To Implement Grep Egrep And Fgrep Commands

Grep:

For example, grep, short for "Global Regular Expression Print," is a robust command-line utility that searches for a specific pattern within a file or a stream of text.

The basic syntax is "grep pattern file", with "pattern" representing the regular expression you want to find and "file" being the specific file you wish to search within.

Egrep:

A more intricate version of this tool is called Egreep. Essentially, it involves executing Grepe with the -E option.

The syntax mimics Grep: egrep matches patterns in files. Utilize the -E flag to interpret extended regular expressions.

Fgrep:

Fgrep is an acronym for "fixed string GREP," indicating its focus on locating a specific string within a file. This utility does not interpret regular expressions; instead, it treats the search pattern as a verbatim string.

The format resembles Grep: fgrep pattern file. It can be employed to locate a specific string without delving into the intricacies of pattern matching procedures.

Implementing Grep, Egrep, and Fgrep in C:

Let's examine the C code for executing these commands at this moment.

Example

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <regex.h>

 

void grep(const char *pattern, const char *filename) {

 FILE *file = fopen(filename, "r");

 if (file == NULL) {

 perror("Error opening file");

 exit(EXIT_FAILURE);

 }

 

 char line[MAX_LINE_LENGTH];

 

 while (fgets(line, sizeof(line), file) != NULL) {

 if (strstr(line, pattern) != NULL) {

 printf("%s", line);

 }

 }

 

 fclose(file);

}

 

void egrep(const char *pattern, const char *filename) {

 regex_t regex;

 int reti = regcomp(®ex, pattern, REG_EXTENDED);

 if (reti) {

 fprintf(stderr, "Could not compile regex\n");

 exit(EXIT_FAILURE);

 }

 

 FILE *file = fopen(filename, "r");

 if (file == NULL) {

 perror("Error opening file");

 exit(EXIT_FAILURE);

 }

 

 char line[MAX_LINE_LENGTH];

 

 while (fgets(line, sizeof(line), file) != NULL) {

 if (regexec(®ex, line, 0, NULL, 0) == 0) {

 printf("%s", line);

 }

 }

 

 regfree(®ex);

 fclose(file);

}

 

void fgrep(const char *pattern, const char *filename) {

 FILE *file = fopen(filename, "r");

 if (file == NULL) {



 perror( "Error opening file");

 exit(EXIT_FAILURE);

 }

 

 char line[ MAX_LINE_LENGTH];

 

 while (fgets(line, sizeof(line), file) != NULL) {

 if ( strstr(line, pattern) != NULL) {

 printf("%s", line);

 }

 }

 

 fclose(file);

}

 

int main(int argc, char *argv[] ) {

 if (argc != 4) {

 fprintf(stderr, "Usage: %s [grep|egrep|fgrep] pattern filename\n", argv[0]);

 exit(EXIT_FAILURE);

 }

 

 const char *command = argv[1] ;

 const char *pattern = argv[2];

 const char *filename = argv[3];

 

 if (strcmp( command, "grep") == 0) {

 grep(pattern, filename);

 } else if (strcmp( command, "egrep") == 0) {

 egrep(pattern, filename);

 } else if (strcmp( command, "fgrep") == 0) {

 fgrep( pattern, filename);

 } else {

 Fprintf (stderr, "Invalid command\n");

 exit(EXIT_FAILURE);

 }

 

 return 0;

}

Examples:

Utilize the Grep command to locate the term "error" within a file.

Example

./text_search grep error sample.log

Utilize Egrep to find lines that start with a numerical pattern using the regular expression "^[0-9]+":

Example

./text_search egrep "^[0-9]+" data.txt

Search for the specified string "apple" within a file by utilizing the Fgrep command:

Example

./text_search fgrep apple fruits.txt

Conclusion:

In summary, leveraging Grep, Egrep, and Fgrep commands in C provides a hands-on experience in text manipulation and applying regular expressions. While showcasing string matching, it serves as a demonstration of how basic programming skills can address various real-world challenges, beginning with essential text search tools.

The C program showcases the flexibility of Grep for fundamental pattern matching, Egrep for extended regex operations, and Fgrep for exact string searches. Exploring the code allows programmers to understand the nuances of file management, string handling, and even explore the intricacies of regex libraries.

Developers looking to broaden their skills can leverage this project to delve into more advanced improvements, test out extra functionalities, or seamlessly incorporate these capabilities into more extensive endeavors. Understanding the inner workings of text search enhances your programming proficiency and empowers you to apply these tools for practical purposes, adding a touch of sophistication to Unix-like systems.

Input Required

This code uses input(). Please provide values below: