user775187
user775187

Reputation: 23441

grep capture regex

I am trying to use grep to capture data below:


"\\.xy$"
"\\.ab$"
"\\.ef\\.hi$"

I have

grep  -Eo "((\\\\\.[a-zA-Z]+)){1,2}\\$" file

two problems:

  1. It can capture things like \\.xy$, but not \\.xy\\.ef$
  2. the returned results have literal $ at the end, why?

Upvotes: 2

Views: 3627

Answers (2)

johnsyweb
johnsyweb

Reputation: 141810

Precede the dollar with a single backslash:

% grep -Eo '"(\\\\\.[[:alpha:]]+){1,2}\$"' input
"\\.xy$"
"\\.ab$"
"\\.ef\\.hi$"

Or put the special characters into square brackets, which I find more readable:

% grep -Eo '"([\]{2}[.][[:alpha:]]+)+"' input 
"\\.xy$"
"\\.ab$"
"\\.ef\\.hi$"

Upvotes: 2

Bohemian
Bohemian

Reputation: 425033

You've double-escaped the $ - try this:

grep  -Eo '"((\\\\\.[a-zA-Z]+)){1,2}\$"' file

Upvotes: 0

Related Questions