2

Based on this question on SO, I have discovered that:

echo "a']" | grep -E "\a"

Matches OK.

echo "a']" | grep -E "\]"

Matches OK.

But:

echo "a']" | grep -E "\'"

Does not match. I cannot find any documentation to describe how exactly grep -E handles escaping a single quote. I am using GNU grep 2.16 on Ubuntu 14.04.

NB:

echo "a']" | grep -E "'"

Matches OK, but I am curious as to how grep -E is interpreting \'.

Update:

I've just tried this on Cygwin 2.6.1 and grep 2.27 and I can reproduce. Using grep --colour -Eo perhaps gives a clearer OK/Fail result?

3
  • 3
    On a similar (14.04, grep 2.16) system, I can not reproduce, grepping "\'" does match. I'm not sure what's going on here, ... Commented Dec 26, 2016 at 2:02
  • 1
    Your update tells about grep -Eo: note that when I add the -o to my grep, nothing's written to stdout (now that's unexpected, AFAIU/not being familiar with that option) yet grep returns with 0 (not 1), which indicates that something was matched. Commented Dec 26, 2016 at 5:06
  • 2
    @SYN: That is the same behavior that you get for grep -o "" or grep -Eo "" — as steeldriver suggested, an empty pattern patches every line, but matches zero characters. Commented Dec 26, 2016 at 5:11

1 Answer 1

7

It's not just GNU grep, with GNU sed and GNU awk: echo "a']" | sed "s/\'/foo/" -> a']foo and echo "a']" | awk "{sub(/\'/,\"foo\")}1" -> a']foo. When you run awk in POSIX mode it does not do that but instead exhibits the expected behavior: echo "a']" | awk --posix "{sub(/\'/,\"foo\")}1" -> afoo].

The issue is that there is a GNU-ism that \' means the same as $, see http://www.regular-expressions.info/gnu.html:

Additional GNU Extensions
....
The anchor \` (backtick) matches at the very start of the subject string,
while \' (single quote) matches at the very end. 

If anyone has a use-case where \' is different from $ I'd love to hear it.

1
  • 2
    Yep, they're GNU operators, more specifically buffer operators Commented Dec 26, 2016 at 19:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.