Changes

Jump to: navigation, search

Tutorial9: Regular Expressions

44 bytes added, 14:24, 2 September 2020
INVESTIGATION 3: OTHER COMMANDS THAT USE REGULAR EXPRESSIONS
=INVESTIGATION 3: OTHER COMMANDS THAT USE REGULAR EXPRESSIONS =
In this section, you will see how regular expressions can be used with other Linux utilities other than just '''grep''' or '''egrep'''.
#Let's use regular expressions with the '''more''' command.<br>Issue the following linux command to view the contents of the '''textfile1.txt''' (downloaded in a previous section):<br><span style="color:blue;font-weight:bold;font-family:courier;">more textfile1.txt</span><br><br>
#We want to search for a pattern '''uli101''' within this text file.<br>Type the following regular expression and press ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">/uli101</span><br><br>What did you notice?<br><br>
#Search for the next occurrence of the pattern '''uli101''' by '''re-typing ''' the following regular expression and pressing ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">/uli101</span><br><br>you should now see the '''second occurrence ''' of this pattern within the text file.<br><br>
# Press the letter <span style="color:blue;font-weight:bold;font-family:courier;">q</span> to exit 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 command to edit the '''textfile1.txt''' file:<br><span style="color:blue;font-weight:bold;font-family:courier;">vi textfile1.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>
# Navigate throughout the text file to see if the other occurrences have been replaced.<br><br>You should notice they haven't for the other two occurrences. In order to replace for ALL occurrences, you need to add the letter g (meaning "'''global'''") at the end of the last forward slash (e.g /'''search/replace/g''').<br><br>
# Making certain that you are command mode in vi, type the following and press ENTER:<br><span style="color:blue;font-weight:bold;font-family:courier;">:%s/uli101/ULI101/g</span><br><br>
# Navigate throughout the text file to confirm that ALL occurrences of uli101 have been replaced with ULI101.<br><br>
13,420
edits

Navigation menu