Use grep to find strings at the beggining of a line or after a delimiter in Git Bash for Windows

I have such file:


I want to use grep to find anything containg blue| at the beginning of line or |blue| anywhere, but not any darkblue| or |darkblue| or |blueberry|

I tried to use grep [^|\|]blue\| but Git Bash gives me error:

$ grep [^|\|]blue\| *.*
grep: Unmatched [ or [^
sh.exe": |]blue|: command not found

What did I do wrong? What's the proper way to do it?


Here's a quick & dirty one:

grep -E '(^|\|)blue\|' *

Matches start of line or |, followed by blue|. The important note is that you need extended regular expressions (via egrep or the -E flag) to use the | (or) construct.

Also, note the single quotes around the regular expression.

So, in answer to the OP's "What did I do wrong?",

  1. You forgot to put the regexp in single quotes;
  2. You chose the wrong type of brackets to enclose the alternate expressions; and finally
  3. You forgot to use egrep or the -E flag

It's always easier to see other people's errors; I wish I was a quick to spot my own :-|

Need Your Help

Repository Pattern For Accessing Text File

c#-4.0 repository repository-pattern

I am fairly new to the Repository Pattern and I would like to do this correctly. I am also trying to make use of Inversion of Control (also new).

Getting authentication token from IP with nunit

authentication nunit wif

I am working on adding WIF support to my WCF Data Services / ODATA server, and the first thing I'd like to do is create a nUnit test which passes some sort of identity to said server. I believe this

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.