Changes

Jump to: navigation, search

Tutorial9: Regular Expressions

81 bytes added, 11:41, 28 February 2021
INVESTIGATION 3: OTHER COMMANDS THAT USE REGULAR EXPRESSIONS
# 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 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;">/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,<br>you need to add the letter g (meaning "'''global'''") at the end of the last forward slash (e.g /'''search/replace/g''').<br><br>
13,420
edits

Navigation menu