...
- here are some good ones:
- a good general one: https://www.regular-expressions.info/
- Ryan's tutorials on Regular Expressions: http://ryanstutorials.net/regular-expressions-tutorial/
- RegexOne: http://regexone.com
- and a perl regex tutorial: http://perldoc.perl.org/perlretut.html
- perl regular expressions are the "gold standard" used in most other languages
perl pattern matching
If grep pattern matching isn't behaving the way I expect, I turn to perl. While Perl, like awk, is a fully functional programming language, Here's how to invoke regex pattern matching from a command line using perl:
perl -n -e 'print $_ if $_=~/<pattern>/'
sed pattern substitution
The sed (string editor) command can be used to edit text using pattern substitution.
sed 's/<search pattern>/<replacement>/'
While sed is very powerful, the regex syntax for its more advanced features is quite different from "standard" grep or perl regular expressions. As a result, I tend to use it only for very simple substitutions, usually as a component of a multi-pipe expression.
perl pattern substitution
If I have a more complicated pattern, or if sed pattern substitution is not working as I expect (which happens frequently!), I again turn to perl. Here's how to invoke perl pattern substitution from a command line:
perl -p -e '~s/<search pattern>/<replacement>/'
Parentheses ( ) around one or more text sections in the <search pattern> will cause matching text to be captured in built-in perl variables $1, $2, etc., following the order of the parenthesized text. The capture variables can then be used in the <replacement>.
Field delimiter summary
Be aware of the default field delimiter for the various bash utilities, and how to change them:
...
Examples:
ls -l ~/.bash_history
haiku.txt | description |
---|---|
|
ls -l /usr/bin/ls
/usr/bin/ls | description |
---|---|
|
ls -l -d ~/local (-d says to list directory information, not directory contents)
docs | description |
---|---|
|
Copying files between TACC and your laptop
Anchor | ||||
---|---|---|---|---|
|
...