Open main menu

CDOT Wiki β

Changes

Tutorial 9 - Regular Expressions

2,764 bytes added, 12:15, 8 June 2023
m
Protected "Tutorial 9 - Regular Expressions": OER transfer ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))
Content under development
 
=USING REGULAR EXPRESSIONS=
<br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
= INVESTIGATION 1: SIMPLE & COMPLEX REGULAR EXPRESSIONS =
<span style="color:red;">'''ATTENTION''': The due date for successfully completing this tutorial (i.e. tutorial 89) is by Friday, December 15 April 21 @ 11:59 PM (Week 14).</span><br>
In this investigation, you will learn how to use the '''grep''' command with '''simple and complex regular expressions'''<br>to help search for ''patterns'' contained in text files.
# Make certain that you are located in your '''~/regexps''' directory on your ''Matrix'' account.<br><br>
# Issue the following Linux command to copy another data file called '''numbers2.dat''':<br><span style="color:blue;font-weight:bold;font-family:courier;">cp ~osl640/tutorial9/numbers2.dat .</nowiki></span><br><br>
# View the contents of the '''numbers2.dat''' file using the '''more''' command and quickly view the contents of this file.<br>You should notice ''valid'' and ''invalid'' numbers contained in this file. When finished, exit the more command.<br><br>[[Image:eregexps-1.png|thumb|right|300px|'''Weakness''' of '''complex''' regular expressions that do not '''limit''' the number of '''positive''' or '''negative''' signs.]]
# Issue the following Linux command to display '''signed''' or '''unsigned integers''':<br><span style="color:blue;font-weight:bold;font-family:courier;">grep "^[+-]*[0-9][0-9]*$" numbers2.dat</span><br><br>You should notice '''multiple''' '''+''' or '''-''' '''signs''' appear <u>prior</u> to some numbers.<br>This occurs since you are searching or one or MORE occurrences of a + or - sign.<br><br>Using '''extended regular expression''' symbols to specify '''minimum''' and '''maximum''' repetitions: '''{min,max}''' can solve that problem.<br><br>
= LINUX PRACTICE QUESTIONS =
== REVIEW QUESTIONS: SIMPLE &amp; COMPLEX REGULAR EXPRESSIONS ==
Here is a link to the [https://matrix.senecacollege.ca/~osl640/questions/osl640_command_practice_9a.docx MS Word Document of ALL of the questions] displayed below but with extra room to answer on the document to simulate a quiz.
13. Write a Linux command to display all lines in the file called '''~/text.txt''' that contains exactly 3 numbers.<br>
14. Write a Linux command to display all lines in the file called '''~/text.txt''' that contains 1 or more “C” characters.<br>
 
== REVIEW QUESTIONS: REGULAR EXPRESSIONS (INCLUDING EXTENDED REGULAR EXPRESSIONS) ==
Here is a link to the [https://matrix.senecacollege.ca/~osl640/questions/osl640_command_practice_9B.docx MS Word Document of ALL of the questions] displayed below but with extra room to answer on the document to simulate a quiz.
 
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
'''Part A: Display Results from Linux Commands using Regular Expressions'''
 
Note the contents from the following tab-delimited file called '''~murray.saul/uli101/numbers.txt''':
 
<pre>
+123
---34
+++++++++++17
-45
45p8
25.6
11
</pre>
 
Write the results of each of the following Linux commands using regular expressions for the above-mentioned file.
 
1. <span style="font-family:courier;font-weight:bold">grep "^[-+]" ~osl640/tutorial9/numbers.txt</span><br>
2. <span style="font-family:courier;font-weight:bold">grep "^[-+]*.[0-9]" ~osl640/tutorial9/numbers.txt</span><br>
3. <span style="font-family:courier;font-weight:bold">grep "^[+-]?[0-9]" ~osl640/tutorial9/numbers.txt</span><br> &nbsp; &nbsp;(Why?)<br>
4. <span style="font-family:courier;font-weight:bold">egrep "^[+-]?[0-9]" ~osl640/tutorial9/numbers.txt</span><br>
5. <span style="font-family:courier;font-weight:bold">egrep "^[+-]?[0-9]+$" ~osl640/tutorial9/numbers.txt</span><br>
6. <span style="font-family:courier;font-weight:bold">egrep "^[+-]?[0-9]+[.]?[0-9]+$" ~osl640/tutorial9/numbers.txt</span><br>
 
 
'''Part B: Writing Linux Commands Using Regular Expressions'''
 
Write a single Linux command to perform the specified tasks for each of the following questions.
 
 
7. Write a Linux command to display all lines in the file called '''~/data.txt''' that begins with 1 or more occurrences of an UPPERCASE letter.
 
8. Write a Linux command to display all lines in the file called '''~/data.txt''' that ends with 3 or more occurrences of the number 6
 
9. Write a Linux command to display all lines in the file called '''~/data.txt''' that begins with 2 or more occurrences of the word “the” (upper or lower case).
 
10. Write a Linux command to display all lines in the file called '''~/data.txt''' that begins with 2 or more occurrences<br> &nbsp; &nbsp; &nbsp; of the word “the” <u>or</u> the word “but” (upper or lower case).
 
11. Write a Linux command to display all lines in the file called '''~/data.txt''' that begins with a minimum of 2 occurrences<br> &nbsp; &nbsp; &nbsp; and a maximum of 4 occurrences of the word “the” or the word “but” (upper or lower case).
 
[[Category:OSL640]]