Using the Jabberwocky poem:
'Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. "Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!" He took his vorpal sword in hand: Long time the manxome foe he sought-- So rested he by the Tumtum tree, And stood awhile in thought. And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came! One two! One two! And through and through The vorpal blade went snicker-snack! He left it dead, and with its head He went galumphing back. "And hast thou slain the Jabberwock? Come to my arms, my beamish boy! O frabjous day! Callooh! Callay!" He chortled in his joy. 'Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe.
Answer the following questions:
Question 1: Find a regular expression that will allow you to grep for all lines containing words that start with "H" and end with "d".
Question 2: Find a regular expression that will match all lines containing a capital "T", but not if capital "T" is the first character in the line.
Question 3: Using sed, replace "'Twas" with "It certainly was" (notice the apostrophe before the ‘T’).
Question 4: Using sed, replace all occurrences of "the" or "The" with "a" or "A" (preserving the capitalization of the original).
Question 5: Find all of the punctuation marks used in the poem, and give a count of the number of occurrences of each.
1) grep '^H.*d$>' poem.txt
we use ^ for specifying starting characters and $ for ending characters
.* is used for specifying the word may contain any number of character inbetween H and d
2) grep '^[^T][T]' poem.txt
[^T] specifies that all matches except T.
^[^T] specifies that all matches except T as a start
[T] specifies all matches contains T
3) sed "s:'Twas/It certainly was:g" poem.txt
sed "s/old-text/new-text/g" input.txt is the syntax
Here we used : instead of / to include space in between the new text 'It certain was'
4) sed 's:The/A:g; s:the/a:g ' poem.txt
To sed multiple pattern in a single call, we use ; to seperate two replacements.
5) grep -o '[[:punct:]]' poem.txt | sort | uniq -c
[:punct:] specifies all punctuation character
[[:punct:]] means all that matches punctuation characters
uniq -c is for counting the number of occurences
Get Answers For Free
Most questions answered within 1 hours.