1. What is a Regular Expression?
A regular expression is a pattern used to match text.
Example text:
file_2024-01-10.log
file_2024-02-11.log
notes.txt
Regex can extract only the date.
Example:
[0-9]{4}-[0-9]{2}-[0-9]{2}
Matches:
2024-01-10
2024-02-11
2. Where Regex is Used in Bash
Common Bash tools using regex:
| Command | Purpose |
|---|---|
grep | search text |
sed | modify text |
awk | pattern scanning |
[[ =~ ]] | bash regex matching |
Example:
grep '[0-9]' file.txt
Find lines containing numbers.
3. Basic Regex Characters
Literal Match
Regex normally matches exact characters.
Example:
cat
Matches:
cat
But not:
cats
scatter
4. The Dot (.)
.
Means:
any single character
Example:
c.t
Matches:
cat
cut
cot
c9t
But NOT:
ct
cart
5. Character Classes
[abc]
Match any one character inside brackets
Example:
gr[ae]y
Matches:
gray
grey
[a-z]
Range match.
Example:
[a-z]
Matches any lowercase letter
Examples:
a
m
z
[A-Z]
Uppercase letters.
[0-9]
Numbers.
Example:
[0-9][0-9]
Matches:
12
45
99
6. Negated Character Class
[^abc]
Means:
anything except a,b,c
Example:
[^0-9]
Matches:
a
B
_
But not:
5
7
7. Anchors
Anchors match position, not characters.
Beginning of line ^
^hello
Matches:
hello world
But not:
say hello
Example in bash:
grep '^ERROR' logfile
End of line $
world$
Matches:
hello world
But not:
world hello
Example:
grep '\.log$'
Matches files ending with .log.
8. Repetition Operators
These define how many times something repeats.
* (zero or more)
ab*
Matches:
a
ab
abb
abbb
+ (one or more)
ab+
Matches:
ab
abb
abbbb
Not:
a
Note: + works in extended regex.
? (zero or one)
colou?r
Matches:
color
colour
9. Exact Repetition {}
Specify exact counts.
Exactly N
[0-9]{4}
Matches:
2024
1999
Range
[0-9]{2,4}
Matches:
12
123
1234
Minimum
[0-9]{2,}
Matches:
12
123
12345
10. Groups ()
Parentheses create groups.
Example:
(abc)+
Matches:
abc
abcabc
abcabcabc
11. Alternation |
Acts like OR.
Example:
cat|dog
Matches:
cat
dog
12. Escaping Special Characters
Some characters are special:
. * + ? ^ $ ( ) [ ] { } |
To match them literally use \.
Example:
Match .log
\.log
13. Bash Built-in Regex Matching
Bash has regex using [[ ]].
Example:
file="log_2024-10-01.txt"
if [[ $file =~ [0-9]{4}-[0-9]{2}-[0-9]{2} ]]
then
echo "date found"
fi
14. Capturing Groups in Bash
Example:
file="backup_2024-01-10.tar"
if [[ $file =~ ([0-9]{4})-([0-9]{2})-([0-9]{2}) ]]
then
echo "Year: ${BASH_REMATCH[1]}"
echo "Month: ${BASH_REMATCH[2]}"
echo "Day: ${BASH_REMATCH[3]}"
fi
BASH_REMATCH array contains matched groups.
15. Real Shell Example
Extract log date.
Example file names:
app-2024-10-01.log
app-2024-10-02.log
error.log
Script:
for f in *.log
do
if [[ $f =~ ([0-9]{4}-[0-9]{2}-[0-9]{2}) ]]
then
echo "Date: ${BASH_REMATCH[1]}"
fi
done
Output:
Date: 2024-10-01
Date: 2024-10-02
No comments:
Post a Comment