In C++ 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!";

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

In C++

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 with 16 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education