Regular expressions, also known as regex, are a powerful tool for matching patterns in text. The grep
command in Unix and Linux supports regular expressions, and can be used to search for patterns in files, directories, or the output of other commands.
Here are some examples of using regular expressions with grep
:
grep "word" input
For example, to search for the word "test" in the file file.txt
, you can use the following command:
grep "test" file.txt
This will print all the lines in the file file.txt
that contain the word "test."
^
symbol. For example:grep "^test" input
This will match any line that starts with the string "test."
$
symbol. For example:grep "test$" input
This will match any line that ends with the string "test."
.
symbol. For example:grep "t.st" input
This will match any line that contains the string "t" followed by any character, followed by "st."
[]
. For example:grep "t[aeiou]st" input
This will match any line that contains the string "t" followed by any vowel, followed by "st."