Regex for "or" of multiple words in grep

Ocasta Eshu
[Computer]$ grep "foo|bar" filename

I understand the above command should return each line in filename where there exits "foo" or "bar". The man pages confirms | as the Regex or symbol and the code works for "foo" and "bar" independently. What am I missing?

jhenninger

grep uses basic regular expressions (BRE) by default. From the man page:

Basic vs Extended Regular Expressions: In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, +, {, \|, (, and ).

So you either have to escape the |:

grep "foo\|bar" filename 

or turn on extended regular expressions:

grep -E "foo|bar" filename

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Using Grep to search for multiple words in a file

分類Dev

Regex for multiple different words, between white spaces

分類Dev

Regex Replace multiple words, use them to build list

分類Dev

Searching for multiple matches on one line using Grep and Regex

分類Dev

Using grep and regex to extract words from a file that contain only one kind of vowel

分類Dev

Grep words with special symbols

分類Dev

chosen search for multiple words

分類Dev

Highlight Words from a Regex Match

分類Dev

Regex Expression for Finding Words Surrounded By {{ }}

分類Dev

Separating words with Regex (Not in specific order)

分類Dev

Regex pattern counting with repetitive words

分類Dev

Regex to select words with spaces for subsititution

分類Dev

Substituting part of words in Perl regex

分類Dev

How to grep multiple @ types?

分類Dev

Python convert list of multiple words to single words

分類Dev

What is the email matching regex in basic regex for grep?

分類Dev

Grep + Regex Linuxの使用

分類Dev

Linux bash: grep with regex error

分類Dev

Why is my grep + regex not working?

分類Dev

Combine multiple consecutive words in python

分類Dev

Split lines with multiple words in Python

分類Dev

grep searching multiple strings in a file

分類Dev

Using grep command for multiple strings

分類Dev

Using Grep to multiple find and replace

分類Dev

Android Studio and regex search (find more words)

分類Dev

How to partial search for words using regex python

分類Dev

RegEx for matching one to three words after a keyword

分類Dev

What is the regex to match the words containing all the vowels?

分類Dev

REGEX: find URL with specific words/pages

Related 関連記事

ホットタグ

アーカイブ