Changes

Jump to: navigation, search

Tutorial9: Regular Expressions

33 bytes added, 08:07, 31 January 2021
INVESTIGATION 1: SIMPLE & COMPLEX REGULAR EXPRESSIONS
# First, issue the following linux command to download another data file called '''numbers1.dat''':<br><span style="color:blue;font-weight:bold;font-family:courier;">wget <nowiki>https://ict.senecacollege.ca/~murray.saul/uli101/numbers1.dat</nowiki></span><br><br>
# View the contents of the '''numbers.dat''' file using the '''more''' command and quickly view the contents of this file. You should notice valid and invalid numbers contained in this file. When finished, exit the more command.<br><br>
# Issue the following linux command to display only whole numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep "^[0-9]*$" numbers1.dat | tee faulty.txt | more</span><br><br>You may have noticed that the command does not entirely work. You may notice an empty line (which is NOT a whole number). This occurs since the * regular expression symbol represents ZERO or MORE occurrences of a number. You can use an additional numeric character class with the * regular expression symbol to search for one or more occurrences of a number.<br><br># Issue the following linux command to display only whole numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep "^[0-9][0-9]*$" numbers1.dat | tee whole.txt | more</span><br><br>You should see that this works.<br><br>
# Issue the following linux command to display whole positive or negative integers:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep "^[+-][0-9][0-9]*$" numbers1.dat | more</span><br><br>What did you notice?<br><br>
# Issue the following linux command to display only whole numbers (with or without a positive or negative sign):<br><span style="color:blue;font-weight:bold;font-family:courier;">grep "^[+-]*[0-9]*$" numbers1.dat | more</span><br><br>
13,420
edits

Navigation menu