Question: Simple grep First released 43 years ago (!), grep is one of the oldest command-line utilities in the UNIX ecosystem. grep is used to print

Simple grep First released 43 years ago (!), grep is one of the oldest command-line utilities in the UNIX ecosystem. grep is used to print the lines that it receives if they match a certain pattern. This tool makes it possible to detect if a file contains a certain word for example, and if so, optionally at which line(s). Write program simple_grep.c that reads entire lines from the user, and only prints back those which match any of the patterns received as program arguments.

Here are a list of requirements, assumptions and hints:

This program shall contain no global variables.

We assume that the maximum number of characters a line can contain is 80.

You will probably need to split the problem into a hierarchy of functions. A possible set of functions could include:

A function that returns whether a line matches a certain pattern.

A function that returns whether a line matches any of pattern in a collectin of pattens (such a function would call the first one).

The main function should be the brain, reading the line from the user and calling the matching function before displaying the line if it is a match.

If there is no given argument on the command, the program should display the commands usage and return 1.

If the first command-line argument is -n, then the line number should be displayed before the matching lines.

In order to match strings while ignoring their case, you can use strncasecmp() function provided by the libc.

List of some important libc functions that are used in the reference program: strlen(), strncasecmp(), strcmp(), fgets().

please check following example.

$ cat udhr_art26.txt(this is a sample text file as an input) (1) Everyone has the right to education. Education shall be free, at least in the elementary and fundamental stages. Elementary education shall be compulsory. Technical and professional education shall be made generally available and higher education shall be equally accessible to all on the basis of merit. (2) Education shall be directed to the full development of the human personality and to the strengthening of respect for human rights and fundamental freedoms. It shall promote understanding, tolerance and friendship among all nations, racial or religious groups, and shall further the activities of the United Nations for the maintenance of peace. (3) Parents have a prior right to choose the kind of education that shall be given to their children. $ ./simple_grep right FREEDOM "their children" < udhr_art26.txt(this is a file name) (1) Everyone has the right to education. Education shall be for human rights and fundamental freedoms. It shall promote (3) Parents have a prior right to choose the kind of education that shall be given to their children. $ ./simple_grep -n right FREEDOM "their children" < udhr_art26.txt 1:(1) Everyone has the right to education. Education shall be 9:for human rights and fundamental freedoms. It shall promote 13:(3) Parents have a prior right to choose the kind of 14:education that shall be given to their children. $ ./simple_grep Usage: grep [-n] word ... $ echo $? 1 $

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!