Regular expressions in grep ( regex ) with examples

http‮.www//:s‬lautturi.com
Regular expressions in grep ( regex ) with examples

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:

  1. To search for a specific word or string, you can use the following syntax:
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."

  1. To search for a pattern that starts with a specific string, you can use the ^ symbol. For example:
grep "^test" input

This will match any line that starts with the string "test."

  1. To search for a pattern that ends with a specific string, you can use the $ symbol. For example:
grep "test$" input

This will match any line that ends with the string "test."

  1. To search for a pattern that contains a specific string, you can use the . symbol. For example:
grep "t.st" input

This will match any line that contains the string "t" followed by any character, followed by "st."

  1. To search for a pattern that contains a specific set of characters, you can use brackets []. For example:
grep "t[aeiou]st" input

This will match any line that contains the string "t" followed by any vowel, followed by "st."

Created Time:2017-10-30 14:27:15  Author:lautturi