I want to use Unix's grep function to pull out multiple lines (with different keywords) from a file in one command line.
For example, I have something like:
doc-A1-151
file-A2-15646
table-A3-1654
file-B1-15654
doc-B2-15654
table-B3-13546
file-C1-164654
doc-C2-16354
table-C3-13565
And I want a sub-version of the file with just the A1, B3, and C2 lines.
How do I do that?
Best Answer
Grep allows you to use regular expressions to match patterns within the file using the
-E
flag, or you can use theegrep
command which is equivalent togrep -E
:or
The vertical bar,
|
, is the OR operator meaning match string A1 or B3 or C2.Regular expression syntax varies from tool to tool, but generally the syntax is the same. Here is a regex test harness for Ruby that I use frequently to test and build regular expressions: http://rubular.com/r/mJyIMO5hJN
However, any introduction to regular expressions should be prefaced with a warning that they are limited in their capabilities, and the adage is: