Implement a simple version of the linux grep command in C++. grep - Looks through a file, line by line, trying to find a user-specified search term in the line. If a line has the word within it, the line is printed out, otherwise it is not. Use the system calls open(), getline(), close().  Requirements (examples run from. terminal) Your program grep is always passed a search term and zero or more files to grep through (thus, more than one is possible). It should go through each line and see if the search term is in it; if so, the line should be printed, and if not, the line should be skipped. [terminal]$ ./grep ! main.cpp main2.cpp cout << "Hello, World!"; cout << "Programming is great fun!"; The matching is case sensitive. Thus, if searching for world, lines with World will not match.  Lines can be arbitrarily long (that is, you may see many many characters before you encounter a newline character, \n). grep should work as expected even with very long lines. For this, you might want to look into the getline() library call. If grep is passed no command-line arguments, it should print "wgrep: searchterm [file ...]" (followed by a newline) and exit with status 1. [termianl]$ ./grep wgrep searchterm [file ...] If grep encounters a file that it cannot open, it should print "grep: cannot open file" (followed by a newline) and exit with status 1. [terminal]$ ./grep World main3.cpp wgrep: cannot open file In all other cases, grep should exit with return code 0. If a search term, but no file, is specified, grep should work, but instead of reading from a file, grep should read from standard input. [terminal$ ./grep World Hello World Hello World Programming is great fun! Programming for real world problems is complex! Programming makes the World go round Programming makes the World go round ^C For simplicity, if passed the empty string as a search string, grep can either match NO lines or match ALL lines, both are acceptable. Here is an example of returning everything. [terminal]$ ./grep "" main.cpp #include using namespace std; int main(){ cout << "Hello, World!"; return 0; } If a search term consists of multiple words, the grep should work as follows: [terminal]$ ./grep "Hello, World" main.cpp cout << "Hello, World!";

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Implement a simple version of the linux grep command in C++. grep - Looks through a file, line by line, trying to find a user-specified search term in the line. If a line has the word within it, the line is printed out, otherwise it is not. Use the system calls open(), getline(), close(). 

Requirements (examples run from. terminal)

  • Your program grep is always passed a search term and zero or more files to grep through (thus, more than one is possible). It should go through each line and see if the search term is in it; if so, the line should be printed, and if not, the line should be skipped.
    • [terminal]$ ./grep ! main.cpp main2.cpp

      cout << "Hello, World!";

      cout << "Programming is great fun!";
  • The matching is case sensitive. Thus, if searching for world, lines with World will not match. 
  • Lines can be arbitrarily long (that is, you may see many many characters before you encounter a newline character, \n). grep should work as expected even with very long lines. For this, you might want to look into the getline() library call.
  • If grep is passed no command-line arguments, it should print "wgrep: searchterm [file ...]" (followed by a newline) and exit with status 1.
    • [termianl]$ ./grep

      wgrep searchterm [file ...]
  • If grep encounters a file that it cannot open, it should print "grep: cannot open file" (followed by a newline) and exit with status 1.
    • [terminal]$ ./grep World main3.cpp

      wgrep: cannot open file
  • In all other cases, grep should exit with return code 0.
  • If a search term, but no file, is specified, grep should work, but instead of reading from a file, grep should read from standard input.
    • [terminal$ ./grep World

      Hello World

      Hello World

      Programming is great fun!

      Programming for real world problems is complex!

      Programming makes the World go round

      Programming makes the World go round

      ^C
  • For simplicity, if passed the empty string as a search string, grep can either match NO lines or match ALL lines, both are acceptable. Here is an example of returning everything.
    • [terminal]$ ./grep "" main.cpp

      #include <iostream>

      using namespace std;

      int main(){

      cout << "Hello, World!";

      return 0;

      }
  • If a search term consists of multiple words, the grep should work as follows:
    • [terminal]$ ./grep "Hello, World" main.cpp

      cout << "Hello, World!";
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Stack operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY