sed's capture grouping ALTERNATION is not working as expected.
While alternation has the lowest precedence, here is the confusion:
echo "abcd_aefghi" | sed -r "s/(a)(b)(c)(d)|(a)(e)(f)(g)/\8/g;" observed result --> _ghi expected result --> error: invalid reference \8 RHS a--bcd | # Logical alternation (Not bitwise!!) #must be different separated logical state processing!! #not involvement of look behind registered reference grouping Counting!! a--efg ;
sed -r "s/(a)((b)(c)(d)|(e)(f)(g))/\8/;" observed result --> _ghi
more confusion if
sed -r "s/(a)(b)(c)(d)()|((a)(e)(f)(g))/\8/g" observed result --> _ehi
also more confusion
sed -r "s/(a)(b)(c)(d)()||||i am Not comment, Whats going here?|||||||((a)(e)(f)(g))/\8/g" observed result --> _ehi expected result --> full parsing error, because "||"
worst is for inside grouping matching
echo "aBcB_aCfC" | sed -r "s/(a)((B)(c)(\1)|(C)(f)(\1))/\4/g;" #Infield twin uppercase's must match! observed result --> aBcB_aCfC expected result --> c_f
perl -pe repeats the same problem!
The problem is that references counting go beyond alternation scope, rather than resetting the counting after recognizing the alternation symbol.
sed version 4.2.2 on Fedora 20.
Of course I give here the very generic-basic problem.
The real script is very complicated long text raw-parsing.
My initial goal here was to print the 4th element in each alternation mass grouping. Now I must split the match, which enlarges the code significantly.
Can someone please reduce my confusion?