find and replace - multiple combinations

Hello all,

I'm trying to replace all possible combinations of specific character occurrences within a string with another character, that is:

For the following string:

ABCAACD

I want to replace all the possible combinations of the character 'A' with the character 'G', so the output should be:

GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

Does anyone know if there is a linux command that can do it?

Thanks a lot for any help!

How about

awk '
        {for (i=1; i<=NF; i++)   if ($i == "A") AC[++C] = i
         for (i=1; i<=C; i++)   {SAV = $0
                                 for (j=i; j<=C; j++)   {$AC[j] = "G"
                                                         print
                                                        }
                                 $0 = SAV
                                }
        }
' FS= OFS= file
GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

Hi,
Or with sed:

$ echo ABCAACD | sed  'h;:bcl;s/A/_/;H;tbcl;g;s/\(.*\)\n.*\n.*/\1/' | sed -n ':deb;s/A/G/p;tdeb' | sed 's/_/A/g'
GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

Regards.

If you want all combinations, why isn't GBCAGCD included in the desired output?

You haven't said what operating system you're using, and some of the suggestions you have received so far will do what was requested on Linux systems, but not on most BSD and UNIX systems.

If you do want all combinations, you might try something more like:

printf '%s\n' ABCAACD A AA AAAA | awk -FA '
{	printf("Input:\t%s\n", $0)
	for(i = 1; i < 2 ^ (NF - 1); i++) {
		x = i
		for(j = 1; j < NF; j++) {
			printf("%s%s", $j, (x % 2) ? "G" : "A")
			x = int(x / 2)
		}
		printf("%s%s", $NF, ORS)
	}
}'

which produces the output:

Input:	ABCAACD
GBCAACD
ABCGACD
GBCGACD
ABCAGCD
GBCAGCD
ABCGGCD
GBCGGCD
Input:	A
G
Input:	AA
GA
AG
GG
Input:	AAAA
GAAA
AGAA
GGAA
AAGA
GAGA
AGGA
GGGA
AAAG
GAAG
AGAG
GGAG
AAGG
GAGG
AGGG
GGGG

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .