1

How do you use grep to do a text file search for a pattern like ABC='123'?

I'm currently using:

grep -rnwi some/path -e "ABC\s*=\s*[\'\"][^\'\"]+[\'\"]" 

but this only finds text like ABC="123". It misses any instances that use single-quotes. What's wrong with my regex?

3
  • for BRE, you would need to use \+ or use the current regex with -E option to use ERE Commented Feb 9, 2017 at 1:59
  • @Sundeep + is not in BRE at all, but GNU grep BRE supports it as an extension. Commented Feb 9, 2017 at 16:50
  • @BenjaminW. since OP used -r I presumed GNU grep Commented Feb 10, 2017 at 1:46

1 Answer 1

1

You are using a PCRE. So, you need the -P flag. So, use this:

grep -rnwi some/path -P "ABC\s*=\s*[\'\"][^\'\"]+[\'\"]" 

We don't need a \\ for single quotes inside the character classes. So, your regex can also be written as:

"ABC\s*=\s*['\"][^'\"]+['\"]" 

Input file:

ABC="123" ABC='123' 

Run grep with your PCRE:

grep -P "ABC\s*=\s*['\"][^'\"]+['\"]" input.txt 

Output:

ABC="123" ABC='123' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.