Generate all possible word with caps and no caps

With use of sed/awk, how can I print all possible combinations of a word with caps/non-caps.

Eg

Applying operation on "cap" should generate output as follows.

cap
CAP
Cap
cAp
caP
CAp
cAP
CaP
echo "cap" | awk ' BEGIN {
        j = 1
} {
        len = length ($0)

        printf "%s\n", toupper($0)
        printf "%s\n", tolower($0)

        while ( j <= len )
        {
                for ( i = 1; i <= len; i++ )
                {
                        if ( i == j )
                                printf toupper(substr($0,i,1))
                        else
                                printf tolower(substr($0,i,1))
                }
                printf "\n"
                for ( i = 1; i <= len; i++ )
                {
                        if ( i == j )
                                printf tolower(substr($0,i,1))
                        else
                                printf toupper(substr($0,i,1))
                }
                printf "\n"
                ++j
        }
} '

--EDIT--

Even though this code will give desired output for word: cap it seems like I missed few combinations & hence will not work for bigger words.

Please disregard this solution.

echo "test" | awk 'function mix(w,v,a) {
   for(i=length(w)-1;i>=0;i--) {
      if(v>=2^i){
          v=v-2^i
          a=toupper(substr(w,i+1,1)) a
      } else a=tolower(substr(w,i+1,1))a
    }
    return a
}
{for(v=0;v<2^length;v++) print mix($0, v)}'

Output:

test
Test
tEst
TEst
teSt
TeSt
tESt
TESt
tesT
TesT
tEsT
TEsT
teST
TeST
tEST
TEST
2 Likes

Thanks it works perfectly. Can you help in achieving this also?

If I echo "mortgage" and manipulate it with sed/awk, I should get output as follows

[Mm][Oo][Rr][Tt][Gg][Aa][Gg][Ee]

Using awk:

$ echo mortgage | awk '{for(i=1;i<NF;i++) printf "[%c%c]",toupper($i),tolower($i); printf "\n"}' FS=
[Mm][Oo][Rr][Tt][Gg][Aa][Gg][Ee]

Like this?

echo "mortage" | awk  '{for (i=1;i<=NF;i++) printf "["toupper($i)$i"]"} END {print ""}' FS=""
[Mm][Oo][Rr][Tt][Aa][Gg][Ee]
1 Like

Thank you it works fine.

A it more complicated. When I echo a word and perform sed/awk, it should be as follows.

echo viagra | awk_operation
Result:
(?i\)\\b[Vv].?[Ii].?[Aa].?[Gg].?[Rr].?[Aa]\\b

Similary
echo good | awk_opearation
Result:
(?i\)\\b[Gg].?[Oo].?[Oo].?[Dd]\\b

Why can't you try modifying Chubler_XL's solution? If you need help we will help you.

Hint there are 3 parts to the output:
(?i\)\\b at the front
the expanded letters
\\b\n at the end

This is probably a good point to mention the -w and -i options of grep:

 $ echo "Every good boy deserves fruit." | grep -iw every

Sure, I had tried by modifying it. But it did not work out. Anyways, I will try another option.

Compare this with the solution you came up with:

$ echo mortgage | awk '{printf"(?i\\)\\\\b";for(i=1;i<NF;i++) printf ".?[%c%c]",toupper($i),tolower($i); printf "\\\\b\n"}' FS=
(?i\)\\b.?[Mm].?[Oo].?[Rr].?[Tt].?[Gg].?[Aa].?[Gg]\\b
1 Like

For trivial cases, recursion could also be used to generate combinations.

$
$
$ cat -n genperm1.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  use warnings;
     4
     5  sub permute {
     6    my ($x, $y) = @_;
     7    my $len = $#$x;
     8    while ($len >= 0 and $x->[$len] eq '1') { $len-- }
     9    exit if $len < 0;
    10    for (my $i=$#$x; $i>=0; $i--) {
    11      if ($x->[$i] eq '0') {
    12        $x->[$i] = '1';
    13        print map {$x->[$_] eq "0" ? lc $y->[$_] : uc $y->[$_]} 0..$#$x;
    14        print "\n";
    15        bless $x; bless $y;
    16        permute ($x, $y);
    17      } else {
    18        $x->[$i] = '0';
    19      }
    20    }
    21  }
    22
    23  my @arr = split//, lc $ARGV[0];
    24  my @bits = map {0} @arr;
    25  print @arr,"\n";
    26  permute (\@bits, \@arr);
    27
$
$ perl genperm1.pl Linux
linux
linuX
linUx
linUX
liNux
liNuX
liNUx
liNUX
lInux
lInuX
lInUx
lInUX
lINux
lINuX
lINUx
lINUX
Linux
LinuX
LinUx
LinUX
LiNux
LiNuX
LiNUx
LiNUX
LInux
LInuX
LInUx
LInUX
LINux
LINuX
LINUx
LINUX
$
$

Thanks a bunch Chubler_XL. It works perfectly. Also, I was able to learn the usage more with your guidance. :slight_smile: Thank you.