Tuesday, October 27, 2015

Pattern matching and regular expressions in Linux Command Line

[1] tldp.org/LDP/GNU-Linux-Tools-Summary
[2] unix.stackexchange.com/questions/28155/find-files-with-certain-extensions

Regular expressions (regex):
PATTERN=re.compile(r'''((?:[^\t "']|"[^"]*"|'[^']*')+)''')

From [1]

[ ] (square brackets)
specifies a range. If you did m[a,o,u]m it can become: mam, mum, mom if you did: m[a-d]m it can become anything that starts and ends with m and has any character a to d inbetween. For example, these would work: mam, mbm, mcm, mdm. This kind of wildcard specifies an “or” relationship (you only need one to match).
{ } (curly brackets)
terms are separated by commas and each term must be the name of something or a wildcard. This wildcard will copy anything that matches either wildcard(s), or exact name(s) (an “or” relationship, one or the other).
For example, this would be valid:
cp {*.doc,*.pdf} ~
This will copy anything ending with .doc or .pdf to the users home directory. Note that spaces are not allowed after the commas (or anywhere else).

From [2]

find -regex ".*\.\(xls\|csv\)"

find -name "*.xls" -o -name "*.csv"

No comments: