-
Notifications
You must be signed in to change notification settings - Fork 12
grep
We use line anchors to specify the exact position in the file where the regular expression should be matched. We use ^
to match regex at the beginning of the line, and $
to match regex at the end of the line.
If we had a file containing these words:
cat dog concatenate dogma category educated boondoggle vindication chilidog
^cat
would match only cat and category, and dog$
would match dog and chilidog
.
is an unrestricted wildcard character, eg. c.t
would match cat, c5t or cQt.
set of character [character]
would, in the case of c[aou]t
, match patterns that start with c. and are followed by an a, or an o, or an u, followed by t. gr8.
*
means zero or infinitely many repetitions of the character behind it. We usually use it together with .
and we write it as .*
so that it means none or infinitely many (*
) of any character (.
).
[]
represent a character class, while ()
represent character group.
[abc]
would match a, or b, or c.
(abc)
would match exactly these three character abc
For example:
a -- Can be captured by [a-z0-9]
.
a-z0-9 -- Can be captured by (a-z0-9)
and then can be referenced in a replacement and/or later in the expression
Curly brackets { } are general repetition quantifiers. They specify a minimum and maximum number of permitted matches. {match the string if at least n times, match the string if not more than n times}
For example: a{2,4}
matches aa, aaa, or aaaa, but not a or aaaaa
{n}
- exactly n times
{n,}
- no maximum limit
Outside a character class, a question mark (?) matches zero or one character in the string. It is the same as using {0,1}.
We use regular expression with grep
$ grep 'c[aou]t' list.txt
$ grep 'cat$' list.txt
$ ps aux | grep spring
grep
has several useful options for adjusting how it matches regular expressions:
-i
match regex as case-insensitive
-v
only show lines that DO NOT match the regex
-r
apply recursively to a group of files or dirs
-A <NUMBER>
display the <NUMBER>
of lines after the regex match
-B <NUMBER>
display the <NUMBER>
of lines before the regex match
-e
match several regexes can be supplied and they will be matched with logical OR
$ grep '^[#;]' dogs-n-cats
will match lines that begin with #
or ;
.
$ grep '[^#;]' dogs-n-cats
, in this case, ^
means negation, so it will match everything that's not #
or ;
.