Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve the following question, which is quite similiar to PROGRAMMING EXERCISE 7.6 . You need not refer to the question in the book. Write a

Solve the following question, which is quite similiar to PROGRAMMING EXERCISE 7.6. You need not refer to the question in the book.

Write a Python program grep.py that searches for a specific string in a list of files that are specified on the command line and prints out all lines in each listed file containing the search string along with the line number of each line.

Your program should take a search string followed a list of file paths. See the example below.

The output should start with a file path, followed by a single space and then (): Finally, the rest of the line should have content of the particular line on which the search string occurs.

First, let us create some example input files and then demonstrate how your program is supposed to work like.

%%file obituary.txt Clarence Dean Spainhower (Obituary) February 12, 1946 - December 15, 2020

Clarence "Dean" Spainhower, 74, of Raceland, went to be with the Lord on Tuesday, December 15, 2020, at Cabell Huntington Hospital. Dean was born February 12, 1946, in Raceland, Ky., a son of the late John Wesley and Zona Ann Ratliff Spainhower. He is also preceded in death by four brothers, Jack, Tom, Paul, and John Spainhower. After graduating from Raceland High School, he went to work for Armco Steel as a brick mason before retiring from AK steel. Dean being devout in his faith was a member of the First Pentecostal Church of Flatwoods. He was also a Kentucky Colonel. Dean was honest and hardworking, and loved his family. He wasn't a man of many words, but when he talked, you listened. He was wise, kind, and witty. He had a love for music; he played bass and steel guitar and sang at various churches in the community with his loving wife and friends. His family and friends will always remember him walking around, humming a tune. We know if we could hear him now, he would sing, "I'm walking with Jesus all the time" "and now I'm feeling mighty fine." Mr. Spainhower is survived by his loving wife, Roenna Neal Spainhower; two sons, Shaun (Penny) Spainhower of Lima, Ohio, and Eric (Jeri Sue) Spainhower of Flatwoods; two sisters, Betty Logsdon and Jean (Delmar) Gillette; four grandchildren, Megan, Dylan, Makayla, and Ryder. A graveside service will be held on Tuesday, December 22 at 1 p.m. at the Bellefonte Memorial Gardens in Russell, Ky., with Pastor Shane Cox officiating. Due to regulations, masks are required for attendees and social distancing is recommended. Arrangements entrusted to Evans Funeral Home in Raceland, Ky.

Published on December 20, 2020

%%file address.txt Kris Kringle, North Pole Homer Simpson, Springfield Ken Thomson, Petertown, Missouri, USA Mary Falk, Cringley, South Pole Joe Pesci, Somewhere, USA

%%file homework.cc int i = 10 int j = 20 string str = "text"

You may wish to use the following snippets to develop your own program.

You may initially comment out the file magic part and reading input arguments from the command line. Instead, you may hardcode your input file path and the search string until you get everything working well. Then you can add the file magic and the capability to read arguments from the command line.

%%file grep.py import sys

search_string = sys.argv[1] all_filepaths = sys.argv[2:]

Given these three input files, we should be able to look for the string ring in all these files. Then your program should print out the following when run with the search string followed by the three input file paths.

>>> !python grep.py ring obituary.txt address.txt homework.cc obituary.txt (7): After graduating from Raceland High School, he went to work for Armco Steel as a brick mason before retiring from AK steel. Dean being devout in his faith was a member of the First Pentecostal Church of Flatwoods. He was also a Kentucky Colonel. address.txt (1): Kris Kringle, North Pole address.txt (2): Homer Simpson, Springfield address.txt (4): Mary Falk, Cringley, South Pole homework.cc (3): string str = "text"

With this output, we can easily tell on which line in a given file the search string ring occurs at least once.

Now try your program with the command right after the blank code cell below!

!python grep.py ring obituary.txt address.txt homework.cc

obituary.txt (7): After graduating from Raceland High School, he went to work for Armco Steel as a brick mason before retiring from AK steel. Dean being devout in his faith was a member of the First Pentecostal Church of Flatwoods. He was also a Kentucky Colonel. address.txt (1): Kris Kringle, North Pole address.txt (2): Homer Simpson, Springfield address.txt (4): Mary Falk, Cringley, South Pole homework.cc (3): string str = "text"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago