In: Computer Science
Using egrep and grep in linux
What sentences.txt contains:
Anything goes here.
Or not
When, why or how.
A bat can die
The dog never does.
Yikes! he proclaimed
A big can of doo doo is gone.
The zebra never yells at x-rays well unless it is proscribed.
Once upon a time.
Farther. Back down the road!
Yes, a time once did exist
Hello there, hope you're having a good day. Let us start off with a brief of what grep and egrep are. grep is a command-line application or utility to check for patterns of text based on a specific regular expression in a specific line by line data. While egrep achieves the same purpose, it does so in a different way using certain characters as special meta characters.
For the specified questions:
1. grep -e "[aeiou][aeiou]" sentence.txt
The following command is broken down as:
-e specifies we are passing a regular expression
[aeiou][aeiou] marks the actual regular expression where the vowels would repeat.
2. grep -Ei '[[:punct:]]' sentence.txt | grep -Eiv [.]
Here, the Ei mode is used as i ignores case while E runs an extended regex. -v mode inverts matches which only returns unmatched lines. The built-in class [[:punct:]] contains all punctuations.
NB: for "." to be used in egrep, give escape character "\"
3.grep -E '[^[:punct:]]$' sentence.txt
Here, in extended regex mode, the [^] marks non existance of given characters where the characters are punctuations. The $ sign marks the end of the line.
4.grep -P '\w+\s+\w+\s+\w+' sentence.txt
Here, grep is launched in -P perl regex mode for easy searching of words and spaces wherein w+ is a word while s+ marks a spacing.
As the question contains 6 subparts but the student hasn't requested all parts to be answered, I am concluding my answers with that note.
I hope this helped you better understand grep. Thank you have a nice day.