0

I'm using Bash and \\+ works with grep but not with sed.

For example if I do

echo "abbbbc"| sed -e s/ab\\+c/def/

I obtain this result : abbbbc

I tried with ' or with " around s/ab\\+c/def/, I obtain the same result.

but if I replace \\+ with *, I obtain : def

I have to change >Atab_TR4682|c0_g1_i1|m.14206 into Atab

If I do echo ">Atab_TR4682|c0_g1_i1|m.14206" | sed -e "s/>*\\([[:alpha:]]\\\*)_.\*/\1/g"

I obtain Atab_TR4682|c0_g1 probably because * could also be used for 0 iteration, but if I replace * by \\+ it doesn't work at all..

Does anyone have an explanation?

Eric Renouf
  • 18,431
DavidB
  • 21

1 Answers1

1

GNU sed accepts an escaped +, so if your sed is GNU-compatible, you can do:

$ echo "abbbbc" | sed 's/ab\+c/def'
def
$ echo "abbbbc" | sed "s/ab\+c/def"
def
$ echo "abbbbc" | sed s/ab\\+c/def
def

The POSIX-specified (more generally available) sed command only uses POSIX BREs (basic regular expressions) by default. If your sed is POSIX-compatible but not GNU-compatible and you want x+ to act as xx*, then you want the -E switch:

$ echo "abbbbc" | sed -E 's/ab+c/def/'
def

Some non-GNU implementations of sed include -r as a synonym for -E, for compatibility with older versions of GNU sed. The -E syntax is POSIX, and recent GNU sed accept both.

Fox
  • 8,193