Changes

Jump to: navigation, search

Tutorial9: Regular Expressions

No change in size, 11:33, 28 February 2021
INVESTIGATION 3: OTHER COMMANDS THAT USE REGULAR EXPRESSIONS
# Make certain that you are located in your '''~/regexps''' directory on your ''Matrix'' account.<br><br>
# Let's look at using regular expressions with the '''man''' command.<br>Issue the following linux command :<br><span style="color:blue;font-weight:bold;font-family:courier;">man ls</span><br><br>
# We want to search for an option that can sort the file listing.<br>Type the following regular expression below and press '''ENTER''':<br><span style="color:blue;font-weight:bold;font-family:courier;">/sort</span><br><br>'''FYI:''' The '''grep''' and '''egrep''' linux Linux commands contain the regular expressions within quotes,<br>but '''most''' other Linux commands specify regular expressions using forward slashes<br>(e.g. <span style="font-weight:bold;font-family:courier;">/regular expression</span> &nbsp; or &nbsp; <span style="font-weight:bold;font-family:courier;">/regular expression</span>).<br><br>
# Scroll throughout the man pages for the ls command to view matches for the pattern "'''sort'''"<br>(You can press '''SPACE''' or key combination '''alt-b''' to move forward and backwards one screen respectively).<br><br>
# Press the letter <span style="color:blue;font-weight:bold;font-family:courier;">q</span> to '''exit''' the ''man'' pages for '''ls'''.<br><br>Let's use regular expressions with the '''more''' command.<br><br>
# Press the letter <span style="color:blue;font-weight:bold;font-family:courier;">q</span> to exit the '''more''' command.<br><br>
# Try the same search techniques with the '''less''' command.<br><br>Does it work the same for the ''less'' command as it did for the ''more'' command?<br><br>
#Let's learn how to perform a simple '''search and replace''' within the '''vi''' utility by using regular expressions.<br>Issue the following linux Linux command to edit the '''large-file.txt''' file:<br><span style="color:blue;font-weight:bold;font-family:courier;">vi large-file.txt</span><br><br>Let's first perform a simple search within this text file.<br><br>
# Type the following and press ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">/uli101</span><br><br>You should move to the '''first occurrence''' of the pattern: '''uli101'''.<br><br>Let's search for the '''uli101''' pattern, but replace it in capitals (i.e '''ULI101''').<br><br>In vi, in order to perform and command, you need to go into last line mode, and then issue a command to apply to the entire text file, followed by a regular expression to search for and a regular expression to replace (i.e. '''/search/replace/''').<br><br>
# Type the following and press ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">:%s/uli101/ULI101</span><br><br>You should have noticed that the first occurrence of uli101 has been changed to ULI101.<br><br>
13,420
edits

Navigation menu