Changes

Jump to: navigation, search

Tutorial9: Regular Expressions

4 bytes added, 21:15, 6 March 2021
INVESTIGATION 2: EXTENDED REGULAR EXPRESSIONS
# 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][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>
# Issue the following Linux command (using extended regular expression symbols)<br>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.1}[0-9]{1,}$" numbers2.dat</span><br><br>'''NOTE:''' most likely, there were '''NO results'''.<br><br>This is due to the fact that the '''grep command was NOT issued correctly to use extended regular expression symbols'''.<br>You would need to issue either '''grep -E''' (or more simply) issue the '''egrep''' command. The egrep command works with<br>'''all''' regular expression symbols, and should be used in the future instead of the older grep command.<br><br>
# Issue the following Linux pipeline command using '''egrep''' instead of ''grep'':<br><span style="color:blue;font-weight:bold;font-family:courier;">egrep "^[+-]{0,1}[0-9]{1,}$" numbers2.dat | tee better-number1.txt</span><br><br>[[Image:eregexps-2.png|thumb|right|300px|Using '''Weakness''' of '''complexextended''' regular expressions that do not expression symbols (such as '''limitrepetition''' the number ) to refine matches of '''positive''' or signed''and 'negative'unsigned'' signsintegers.]]You should have noticed that the command worked correctly this time because you used the '''egrep''' command.<br><br>'''NOTE:''' In extended regular expressions, the '''?''' symbol can be used to represent the '''{0,1}''' repetition symbols<br>and the '''+''' symbol can be used to represent the '''{1,}''' repetition symbols<br><br>
# Issue the following Linux pipeline command using the repetition shortcuts <span style="font-weight:bold;font-family:courier;">"+"</span> and <span style="font-weight:bold;font-family:courier;">"?"</span>:<br><span style="color:blue;font-weight:bold;font-family:courier;">egrep "^[+-]?[0-9]+$" numbers2.dat | tee better-number2.txt</span><br><br>You should have seen the same results, but the extended regular expression required less typing.<br><br>
# Issue the following Linux pipeline command to display '''signed''', '''unsigned''', '''whole''', and '''decimal''' numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">egrep "^[+-]{0,1}[0-9]{1,}[.]{0,1}[0-9]*$" numbers2.dat | tee better-number3.txt</span><br><br>Were all signed and unsigned intergers and decimal numbers displayed?<br><br>
13,420
edits

Navigation menu