KandZ – Tuts

We like to help…!

Linux CLI 41 🐧 regular expressions

a - regular expressions and POSIX metacharacters 1/2
Regular expressions (regex) are powerful tools for pattern matching and text manipulation.
You can use regex with various commands like grep, sed, awk, and even directly in the shell.
You can use literal characters(abcdef...) but also metacharacters
Basic regex metacharacters (BRE) β†’ ^ $ . [ ] *
. β†’ matches any character - f..d matches food but not foot.
^ β†’ matches at the start of the line. ^foo matches lines that start with foo If inside [ ] then means negation - [^0-9] matches any non-digit.
$ β†’ matches at the end of the line - bar$ matches lines that end with bar
[ ] β†’ matches any characters within the brackets - [aeiou] matches any vowel
'*' β†’ matches 0 or more occurrences - fo*d matches fd, food, and fod but not fed

b - regular expressions and metacharactes 2/2
Extended regex (ERE) β†’ ( ) { } ? + |
( ) β†’ grouping and allows for subexpressions and backreferences - (foo)bar matches foobar
{n} β†’ matches exactly n occurrences of the preceding element - o{2} matches two o's
{n,m} β†’ matches between n and m occurrences of the preceding element - o{1,3} matches one to three o's
? β†’ matches 0 or one occurrence of the preceding element - fo?d matches fd and fod, but not food
'+' β†’ matches one or more occurrences of the preceding element - fo+` matches food and fod, but not fd
| β†’ alternation/or, matches one of the two expressions - foo|bar matches foo or bar

c - regular expressions and POSIX character classes
[:alnum:] β†’ matches any alphanumeric character (letters and digits).
[:alpha:] β†’ matches any alphabetic character (letter). [:digit:] β†’ matches any digit.
[:lower:] β†’ matches any lowercase letter. [:upper:] β†’ matches any uppercase letter.
[:blank:] β†’ matches a space or a tab character.
[:space:] β†’ matches any whitespace character (spaces, tabs, newlines, etc.).
[:graph:] β†’ matches any graphical character (letters, digits, punctuation, and symbols).
[:print:] β†’ matches any printable character. [:punct:] β†’ matches any punctuation character.
[:xdigit:] β†’ matches any hexadecimal digit (0-9, a-f, A-F).

d - regular expressions examples
grep -E '[0-9][0-9]' numbers.txt β†’finds lines containing two consecutive digit
grep -E '^[aeiouAEIOU]' vowels.txt β†’finds lines that start with a vowel (a, e, i, o, u)
sed -E 's/f([a-z])o\1d/b&r/' input.txt output.txt β†’ replaces all occurrences of foo with bar
awk '/^[0-9]/ && /a/' data.txt β†’ prints lines that start with a number and contain the letter a
awk '{gsub(/f([a-z])o\1d/, "b&r"); print}' input.txt output.txt β†’replaces all occurrences of foo with bar
grep -E '\.com' domains.txt β†’finds lines containing the string .com
\ β†’ escapes special characters for example \. \\
Regex operations can be slow on large files.

Leave a Reply