Create a separate directory (lab07 is a good name) for this week's exercises.
The file /home/cs1721/public_html/labs/grep/dracula.txt contains the text of Bram Stoker's Dracula courtesy Project Gutenberg.
The Unix program grep prints the lines in a given file which match a given file. For example:
% grep silver /home/cs1721/public_html/labs/grep/dracula.txt stems shining like silver through the delicate green of the leaves. about him anywhere. He held in his hand an antique silver lamp, in which and silver in the next room, and as I passed through, noticed that Here and there are silver threads where the rivers wind in deep such a silvery, musical laugh, but as hard as though the sound the Demeter. She is almost entirely in ballast of silver sand, On 6 July we finished taking in cargo, silver sand and boxes of earth. the silver light of the moon struck a half-reclining figure, snowy white. and clouded silver-plating gave back the feeble glimmer of a candle, He held up a little silver whistle, as he remarked, "That old place silver crucifix and held it out to me, I being nearest to him, little silver whistle from his pocket, he blew a low, shrill call.
If the option -n is specified, grep precedes each line it prints with its line number. For example:
% grep -n gold /home/cs1721/public_html/labs/grep/dracula.txt 820:The table service is of gold, and so beautifully wrought 1573:fair,as fair as can be, with great masses of golden hair 1767:gold piece, and made what signs I could to have them posted. The man who took 2002:The only thing I found was a great heap of gold in one corner, 2003:gold of all kinds, Roman, and British, and Austrian,and 2169:I could not see a key anywhere, but the heap of gold remained. 2264:I shall take some of the gold with me, lest I want it later. 3314:sunset colour, flame, purple, pink, green, violet, and all the tints of gold, 5195:and the sunshine has not yet begun to paint him with his gold, 7198:a little gold crucifix, and placed it over the mouth. 7279:held up the little golden crucifix. 9343:sprang forward and held between them his little golden crucifix. 10502:The Professor stood up and, after laying his golden 12555:golden crucifix, and said with wonderful calmness, "Do not fear, my dear. 13515:wide gap whence a bundle of bank notes and a stream of gold fell out. 13530:Through the sound of the shivering glass I could hear the "ting" of the gold, 15455:been duly cashed for gold at the Danube International Bank.
If the option -i is specified, grep ignores the case of letters when matching the pattern. In other words, an upper case letter will match a lower case letter and vice-versa. For example:
% grep secondly /home/cs1721/public_html/labs/grep/dracula.txt have to be, no other means is at our control, and secondly, % grep Secondly /home/cs1721/public_html/labs/grep/dracula.txt Secondly we must see, as well as we can surmise it from the facts we know of, % grep -i secondly /home/cs1721/public_html/labs/grep/dracula.txt have to be, no other means is at our control, and secondly, Secondly we must see, as well as we can surmise it from the facts we know of, % grep -i SECONDLY /home/cs1721/public_html/labs/grep/dracula.txt have to be, no other means is at our control, and secondly, Secondly we must see, as well as we can surmise it from the facts we know of, % grep -i -n gIb /home/cs1721/public_html/labs/grep/dracula.txt 1195:or that it is a medium, a tangible help, in conveying memories of sympathy 3643:in bad weather. Passed Gibraltar and out through Straits. All well. 7748:properly intelligible account of themselves, but the consensus 15384:the deil a thing could we see. We ran by Gibraltar wi' oot bein'
Write a program called grep.c which emulates the Unix program grep. You only need implement the features mentioned above, i.e. the -i and -n options. You can assume, as above, a single file is specified.
For example:
% a.out Breakfast /home/cs1721/public_html/labs/grep/dracula.txt Breakfast is ready, and we must all eat that we may be strong." Breakfast was a strange meal to us all. We tried to be cheerful and % a.out -n elephant /home/cs1721/public_html/labs/grep/dracula.txt 5947:in one of the cottages in the enclosure behind the elephant house, 8451:why the elephant goes on and on till he have sees dynasties, 11987:How would you like to breakfast on an elephant?" 11992:"I wonder," I said reflectively, "what an elephant's soul is like!" 11997:"I don't want an elephant's soul, or any soul at all!" he said. % a.out -i directory /home/cs1721/public_html/labs/grep/dracula.txt There were even such books of reference as the London Directory, the "Red" of Mitchell, Sons, & Candy from a directory at the Berkeley, % a.out -i -n camp /home/cs1721/public_html/labs/grep/dracula.txt 1742:and are encamped in the courtyard. These are gipsies. 2643:We've told yarns by the campfire in the prairies, and dressed 2647:to be drunk. Won't you let this be at my campfire tomorrow night? 10643:and we must proceed to lay out our campaign. We know from 14326:We went at once into our Plan of Campaign. 14420:of the plans formed for the campaign against the Count.
Your program should print a suitable error message if the file does not exist.
For example:
% a.out directory /homee/cs1721/public_html/labs/grep/dracula.txt Grep: /homee/cs1721/public_html/labs/grep/dracula.txt: No such file or directory
The character '.' should match any single character.
The character '*' should cause zero or more instances of the previous character or regular expression to be matched.
The character '|' causes either the regular expression on its left to be matched or the regular expression on its right to be matched.
The characters '(' and ')' can be used to group regular-expressions.
Part of the challenge is determining the semantics of these meta-characters. The Unix command egrep implements all of these metacharacters. Use it to determine the semantics of these metacharacters. Don't expect your tutor to explain them - you have to figure them out.
Here are some examples of the meta-characters in use:
% a.out 'g.h.i' /home/cs1721/public_html/labs/grep/dracula.txt should be so injured that the very essence of its strength is gone. He have always the strength in his hand of twenty men, even we the whitening hair. % a.out 'sue*cce*de*d' /home/cs1721/public_html/labs/grep/dracula.txt to comfort me. Well, she succeeded somewhat, for, though sympathy I tried to keep awake, and succeeded for a while, but when the clock struck I tried to pacify her, and at last succeeded, and she lay quiet. He has succeeded after all, then, in his design in getting The effort succeeded, for an instant he unconsciously relapsed into his has succeeded. We have had every deference shown to us, % a.out 'z..*z' /home/cs1721/public_html/labs/grep/dracula.txt and before any impulse of violence could seize him he realized the place terror and half in amazement, he gazed at a tall, thin man, is as sunshine. It opens the gate to me. I am dazed, I am dazzled, so much that again I am dazzled, dazzled more than ever, and I must think. "Czarina Catherine reported entering Galatz at one o'clock today." which would arrive at Galatz in the Czarina Catherine. % a.out 'z.*u.*ut' /home/cs1721/public_html/labs/grep/dracula.txt of immense size, and full of beautiful and romantic bits. has seized him. If so, we must look out for squalls, for a strong himself zoophagous, and in his wild ravings outside the chapel door could not wake her. I tried to hypnotize through her sleep, but she The Szgany must look out if they mean to fight. Oh, if only % a.out 'z.*uts' /home/cs1721/public_html/labs/grep/dracula.txt himself zoophagous, and in his wild ravings outside the chapel door % a.out 'a.*at.*ate.*at.*a' /home/cs1721/public_html/labs/grep/dracula.txt as death or fate, so that a thin streak of white mist, that crept % a.out 'yellowed|zeal|yelping' /home/cs1721/public_html/labs/grep/dracula.txt On a table in the hall was a great bunch of keys, with a time-yellowed It was answered from behind Dr. Seward's house by the yelping of dogs, Your police must indeed be zealous men and clever, oh so clever, the Pass. At this time and place, she become all on fire with zeal. % a.out 'w.*(ee|oo)ts' /home/cs1721/public_html/labs/grep/dracula.txt from the train and the little I could walk through the streets. They wore high boots, with their trousers tucked into them, I long to go through the crowded streets of your mighty London, whose roots clung in cracks and crevices and crannies of the stone. in the brilliant moonlight, my own footsteps marked where I had the town, sometimes in rows where the streets are, and sometimes singly. I was asleep, passing through the streets and over the bridge. When we were alone and had heard the last of the footsteps except where there were recent footsteps, in which on holding down and all untouched save for my own footsteps when I had made my first visit. % a.out 'w(ee|oo)*t(ee|oo)*(n|e)*(s| )*o' /home/cs1721/public_html/labs/grep/dracula.txt It was like the intolerable, tingling sweetness of waterglasses That quite won me, Mina, for it was brave and sweet of him, and noble too, poor Arthur, to have lost such a sweetness out of his life! myself with a few happy words with that sweet soul Madam Mina. were of the intolerable sweetness of the water glasses, "Come, sister.
After your tutor has checked that you answers are correct and given you the lab mark, you must also submit your answers using give. Here is the command to use:
% /home/cs1721/bin/classrun -give lab07 grep.c