2. [12 marks This question asks you to use regular expressions For each of the f
ID: 3880580 • Letter: 2
Question
2. [12 marks This question asks you to use regular expressions For each of the following questions, develop a single command line to perform the task required, and use script to record your most successful execution of the command line in a file. Your command lines must work no matter what the current working directory is The names of the files to be generated by script, for the four questions below, are a2q2 a.txt, a2q2_b.txt, a2q2_c.txt and a2q2_d.txt You are only allowed to use the following UNIX commands: grep, ls, cat and wc. You will lose half the marks if you use a command not in this list. Your solutions need not make use of all these commands. The first three questions below ask you to search for English words in a dictionary file. Each line of this file contains an English word. This dictionary file can be found at /usr/share/dict/linux.words on bluenose (a) Print all the words in this dictionary file that start with un and end with ix. This includes the word unix itself. (b) Find all the words in this dictionary file that contain all four lower-case characters that appear in the word "unix" (ie, 'u, ‘n, T, and X) in alphabetical order For example, influx is such a word, but unfix is not (c) Print a single integer that is the number of 6-character words in this dictionary file that start with can and end with either a or c. For example, canada is such a word, but canzona, cancel and camera are not (d) List all the files in /usr/bin whose filenames contain lowercase English alphabet characters only and also contain the word file as a (contiguous) substring. For example, file and nsrfile are such files, but grub2-file is notExplanation / Answer
a. grep "^un[[:alnum:]]*ix$" /usr/share/dict/linux.words > a2q2_a.txt
grep command is used for search a regular expression in a file. ^ anchor is used to specify the pattern that should be present at the beginning of the word. $ for the pattern at the end of the word. [[:alnum:]]* for zero or more alphanumeric characters in between.
b. grep "[[:alnum:]]*i[[:alnum:]]*n[[:alnum:]]*u[[:alnum:]]*x[[:alnum:]]*" /usr/share/dict/linux.words > a2q2_b.txt
Searches for words which contain i,n,u,x in alphabetical order
c. grep "^can[[:alnum:]][[:alnum:]][ac]$" /usr/share/dict/linux.words | wc -w > a2q2_c.txt
Searches for words starting with can, ending with a or c with exactly 2 alphanumeric characters in between. This output is sent as input is sent to wc -w, which returns the number of words
d. ls /susr/bin/ | grep "^[a-z]*file[a-z]*" > a2q2_d.txt
ls /usr/bin/ lists all files in /usr/bin directory. This is taken as input to grep which searches for all words with lowercase characters and the contiguous substring file in it
While checking these command in terminal start with script filename to record the script. After completing type exit to stop recording the script
Hope this helps. Do Upvote! :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.