Lecture: sed, grep, rename
General idea for the lecture, to be properly formatted later.
The common theme are regular expressions, so we will start by explaining the basics about them and then move on to practical examples with the tools sed, grep and rename.
Regular expressions
(Globbing is a simplistic form of regex matching.)
Pattern matching on strings.
This must be relatively quick, so we show the building blocks and basic examples. We'll skip all formal languages theory.
- Quantification:
*
+
?
- Or:
abc|xyc
- Grouping:
(abc)*abc
- Character classes:
.
\d
\s
- More character classes:
[a-z0-9]
[^.-]
- Start/end positions:
^
$
- Matching (same syntax as grouping)
Write regular expressions interactively: http://www.regexr.com/
We start with grep, since it is easier:
- We can use matching and references in sed
- In GNU sed (standard on Ubuntu) many regex operators have to be escaped, unless
-r
is used.
grep
sed
Uses POSIX basic regular expressions, unless -r
is used. Can use alternative delimiters.