0

I cannot seem to find a clever way of creating all possible variations of name combinations.

Input (multiple names put together where each name is represented as a letter):
ABC (e.g. JohnPeterSarah)

pattern space: Replaces A with 1, B with 2 and C with 3 (among other -that's where you come in)

Desired Output
ABC
A2C
A23
AB3
1BC
12C
1B3

Hippo
  • 13
  • 4
  • do you need to use sed? – Grady Player Feb 12 '18 at 21:13
  • sed or awk are preferable because I want to improve my skills within these 2 languages. – Hippo Feb 12 '18 at 21:28
  • What did you try so far? – user unknown Feb 12 '18 at 23:57
  • well so far, I have been using: sed -i.bak -nf script list.txt where the script contains many of these types of codes: s/name1/name2/p. Then I can manually add the original i.bak. without -n I get a lot of dupplicates and I'm not good enough to create a script that does everything on the same time. – Hippo Feb 13 '18 at 13:27

2 Answers2

2

sed is not the most natural tool for this task. Try bash's brace expansion:

$ printf "%s\n" {A,1}{B,2}{C,3}
ABC
AB3
A2C
A23
1BC
1B3
12C
123
John1024
  • 109,961
  • 14
  • 137
  • 171
  • This a great solution (which also shows a desired result that I have't included :-)) But it was just an example. The txt file with names includes 1000s of combinations and also various teams e.g. ABC, ADB, GKE but ONLY some of the names may be switched out by a number. So in my previous example there is only substitute for ABC (123), this means that the new list should incl. the ones you found + ADB and its alterrations + GKE (no alterations). I hope this is more clear... – Hippo Feb 12 '18 at 21:54
  • It is not clear to me what you are asking but, if some letters are to be substituted for but not others, try as an example: `printf "%s\n" ADB{A,1}G{B,2}K{C,3}E`. – John1024 Feb 12 '18 at 22:06
0

Its not clear what you are trying to do here but it sounds like you want all permutations of a string. The best way to do that is using recursion. This question was already addressed here.

Cogitator
  • 154
  • 1
  • 1
  • 1
    Thanks but the answer is in c# as far as I can tell and I would like to further manipulate the results so I would like to be in a language I'm familiar with incl. regular expressions and piping etc. – Hippo Feb 12 '18 at 21:34
  • AB3 is not a permutation of ABC. Permutations are reorderings. (ABC-> ACB, CAB, ...) – user unknown Feb 12 '18 at 23:56