Unix - Regular Expressions
Unix regular expressions are a powerful tool used to match patterns in text. Regular expressions are used in many Unix utilities, including the stream editor (SED), to manipulate text.
SED is a powerful text editor that can perform a variety of tasks, including searching for and replacing text using regular expressions. Here are some of the most commonly used Unix regular expressions with SED:
. (dot): matches any single character except a newline. For example, s.t will match "sat", "set", "sot", and so on.
* (asterisk): matches zero or more occurrences of the previous character or expression. For example, ab*c will match "ac", "abc", "abbc", "abbbc", and so on.
+ (plus): matches one or more occurrences of the previous character or expression. For example, ab+c will match "abc", "abbc", "abbbc", and so on, but not "ac".
? (question mark): matches zero or one occurrence of the previous character or expression. For example, ab?c will match "ac" or "abc".
[ ] (bracket expression): matches any one of the enclosed characters. For example, [abc] will match "a", "b", or "c".
[^ ] (negated bracket expression): matches any character that is not in the enclosed set. For example, [^abc] will match any character except "a", "b", or "c".
\ (backslash): used to escape special characters. For example, s/\./\\./g will replace all occurrences of "." with "." in the text.
These regular expressions can be combined in a variety of ways to match complex patterns in text. For example, s/^[0-9]+//g will replace all numbers at the beginning of a line with an empty string.
In SED, regular expressions are typically used in conjunction with the s (substitute) command. The basic syntax of the s command is s/regexp/replacement/flags, where regexp is the regular expression to be matched, replacement is the text to replace it with, and flags are optional modifiers that control how the substitution is performed. For example, s/abc/def/g will replace all occurrences of "abc" with "def" in the text.