Open main menu

CDOT Wiki β

Changes

Tutorial 9 - Regular Expressions

176 bytes added, 20:42, 25 October 2021
INVESTIGATION 3: OTHER COMMANDS THAT USE REGULAR EXPRESSIONS
# 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 '''less''' command.<br><br>
# Issue the following Linux command to download copy another data file called '''large-file.txt''':<br><span style="color:blue;font-weight:bold;font-family:courier;">wget <nowiki>https://ict.senecacollege.ca/cp ~murray.saulosl640/uli101tutorial9/large-file.txt</nowiki>.</span><br><br>[[Image:other-re-2.png|thumb|right|300px|Entering '''/uli101''' in the '''less''' command can display all matches of "'''uli101'''" throughout the text file. This is just sample output, you will be searching for '''osl640''' instead.]]
# Issue the following Linux command to view the contents of the '''large-file.txt''':<br><span style="color:blue;font-weight:bold;font-family:courier;">less large-file.txt</span><br><br>
#We want to search for a pattern '''uli101osl640''' within this text file.<br>Type the following regular expression and press ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">/uli101osl640</span><br><br>You should see the pattern "uli101osl640" throughout the text file.<br><br>
# Press the letter <span style="color:blue;font-weight:bold;font-family:courier;">q</span> to exit the '''less''' command.<br><br>
# Try the same search techniques with the '''more''' command.<br><br>Does it work the same for the ''less'' command?<br><br>[[Image:other-re-3.png|thumb|right|300px|Entering '''/uli101''' in the '''vi''' command can search for the string "'''uli101'''". This is just sample output, you will be searching for '''osl640''' instead.]]Let's learn how to perform a simple '''search and replace''' within the '''vi''' utility<br>by using regular expressions.<br><br>
# Issue the following 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>
# Press the '''ESC''' key to make certain you are in '''COMMAND''' mode.<br><br>
# Type the following and press '''ENTER''':<br><span style="color:blue;font-weight:bold;font-family:courier;">/uli101osl640</span><br><br>You should notice the pattern "'''uli101osl640'''" highlighted for ALL occurrences in this text file.<br><br>Let's '''search''' for the '''uli101osl640''' pattern, and '''replace''' it in capitals (i.e '''ULI101OSL640''').<br><br>In vi, to issue a command, you need to enter '''LAST LINE''' MODE then issue a command.<br>Let's issue a command from '''LAST LINE''' MODE to search and replace '''uli101osl640''' to '''ULI101OSL640'''.<br><br>[[Image:other-re-4.png|thumb|right|500px|In l'''ast line''' MODE in the '''vi''' text editor, issuing a command using regular expressions to convert '''uli101''' to '''ULI101'''. This is just sample output, you will be searching for '''osl640''' instead.]]# Making certain that you are '''COMMAND''' MODE in vi,<br>type the following and press '''ENTER''':<br><span style="color:blue;font-weight:bold;font-family:courier;">:%s/uli101osl640/ULI101OSL640/g</span><br><br>'''NOTE:''' The letter '''g''' after the replace regular expression represents "'''global'''" and will replace ALL occurrences of uli101 osl640 in the text document (as opposed to replacing the first occurrence for every line).<br><br># Type the following (in uppercase letters) and press '''ENTER''':<br><span style="color:blue;font-weight:bold;font-family:courier;">/ULI101OSL640</span><br><br>You should notice the pattern "'''ULI101OSL640'''" highlighted for ALL occurrences in this text file.<br><br># Navigate throughout the text file to confirm that ALL occurrences of '''uli101osl640''' have been <u>replaced</u> with '''ULI101OSL640'''.<br><br>
# Save changes to your vi editing session and exit by typing the following and pressing ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">:x</span><br><br>
: After you complete the '''Review Questions''' section to get additional practice.