Answer the following questions about regular expressions in UNIX: a) Give the co
ID: 3605705 • Letter: A
Question
Answer the following questions about regular expressions in UNIX:
a) Give the command to search the file ‘usernames.txt’ for all lines ending with ‘goodbye’ or ‘later’ or ‘farewell’
Give regular expressions (such that could be used with grep and egrep) for each of the following:
b) All lines beginning with the phrase ‘Unix is ’
c) All lines that end in either ‘a’, ‘b’, or ‘c’
d) All lines that contain the word ‘result’ followed by any three numbers
e) All lines that contain either 0 or 1 instances of the character ‘x’, but no more
f) All lines that contain a word with 0 or more of the letter ‘a’, followed by 1 or more of the letter ‘b’, followed by 0 or 1 letter ‘c’
Explanation / Answer
a)
grep 'goodbye$|later$|farewell$' usernames.txt
$ means end of line
and then words$ means ending with given word
b)
grep "Unix is " usernames.txt
c)
grep 'a$|b$|c$' usernames.txt
d)
egrep "result[0-9]{3}" usernames.txt