Open main menu

CDOT Wiki β

Changes

Tutorial9: Regular Expressions

4 bytes added, 14:03, 27 February 2021
INVESTIGATION 1: SIMPLE & COMPLEX REGULAR EXPRESSIONS
# View the contents of the '''numbers.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>
# 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</span><br><br>You may have noticed that the command does not entirely work. You may notice an empty line<br>(which is NOT a whole number). This occurs since the * regular expression symbol represents<br>ZERO or MORE occurrences of a number. You can use an additional numeric character class<br>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</span><br><br>You should see that this now 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 | tee signed.txt</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 | tee all.txt</span><br><br>Did this command work?<br><br>
13,420
edits