Open main menu

CDOT Wiki β

Changes

Tutorial9: Regular Expressions

3 bytes removed, 12:19, 27 February 2021
INVESTIGATION 1: SIMPLE & COMPLEX REGULAR EXPRESSIONS
#Issue the following linux pipeline command to match the pattern the within '''textfile1.txt''':<br><span style="color:blue;font-weight:bold;font-family:courier;">grep "the" textfile1.txt</span><br><br>Take a few moments to view the output and observe the matched pattern.<br><br>
# Now, issue the grep linux pipeline command with the '''-i''' option to ignore case sensitively:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep -i "the" textfile1.txt</span><br><br>What do you notice is different with this pipeline command?<br><br>You will notice that the pattern "'''the'''" is matched including larger words like "'''them'''" and "'''their'''".<br>You can issue the '''grep''' command with the -w option to only match the pattern as a '''word'''.<br><br>
# Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep -w -i "the" textfile1.txt</span><br><br>You should now see only strings of text that match the word '''"the"''' (upper or lower case).<br><br>Matching literal or simple regular expressions can be useful, but are '''limited'''<Brbr>in what they can assist with pattern matching.<br>For example, you may want to search for pattern at the '''beginning''' or '''end''' of the string.<br><br>There are other regular expression tools to provide more precise matches. These tools are '''complex''' and '''extended''' regular expressions. We will now look at complex regular expression symbols now, and we will discuss ''extended regular expressions''''''Italic text'''' in the next section of this tutorial.<br><br>
# Issue the following Linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep -w -i "^the" textfile1.txt</span><br><br>The '''^''' symbol is an anchor. In this case, it only matches the <u>word</u> "the" (both upper or lowercase) at the beginning of strings.<br>The '''$''' symbol is used to anchor patterns at the end of strings.<br><br>
# Issue the following Linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep -w -i "the$" textfile1.txt</span><br><br>What do you notice?<br><br>
13,420
edits