'substitute characters in a string in all possible combinations

Character substitution in all possible combinations

I have string as follows,

XGXXGXXGGXXGXXGX

I would like to iteratively substitute X's for either one, two, three or four . (dots) if they satisfy the following conditions,

  1. Substitution in "XGGX" is not allowed.
  2. More than two substitutions on either side of "XGGX" disallowed.
  3. Substitutions that result in ".G." are disallowed.

Really appreciate any help. I tried awk splitting, assigning each character to array and then sed for substitution but it doesn't output as intended.

The output should look like,

.GXXGXXGGXXGXXGX
.GX.GXXGGXXGXXGX
.GX.GXXGGXXGX.GX

and so on.



Solution 1:[1]

I'm not sure if I understand your question correctly, but I have made a file, containing all combinations of a, b and c, and at the end I added some cases where not all of those letters are present:

abc
acb
bac
bca
cab
cba
aaaa
bbbb
cccc
aabb
ccdd
ddee

In order to get the lines which contain all combinations, I just did the following:

cat test.txt | grep "a" | grep "b" | grep "c" | grep -o "[abc]*"

This means:

  • | grep "a" | grep "b" | grep "c" : only show lines which contain AND 'a' AND 'b' AND 'c'
  • grep -o "[abc]*" : show the possible combinations.

The result is as follows:

abc
acb
bac
bca
cab
cba

Is this what you are looking for?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominique