You're viewing a comment by Henning and its responses.

Henning Permalink
April 07, 2013, 17:27

Power set:

$ cat set
$ eval echo {$(cat set | xargs -I{} echo -n \{{},\})}
{abc} {ab} {ac} {a} {bc} {b} {c} {}

or slightly shorter:

$ eval echo {$(cat set | xargs -I# echo -n \{#,\})}

I've got the idea when I saw the wonderful combinatorical feature mentioned under #6 in this article. First I found (after some tries) this which would give the desired result:

$ echo {{,a}{,b}{,c}}
{} {c} {b} {bc} {a} {ac} {ab} {abc}

(The outmost braces are nessesary for the empty set.) But here we manually type in the members of the set. To build the expression from the content of the file "set" I went the following steps:

$ cat set |xargs -I{} echo \{{},\}

We need it in one line, so we append -n to echo:

cat set |xargs -I{} echo -n \{{},\}

(We dont get a newline after that!)
To surround this with curly brackets we use command substition and another echo:

$ echo {$(cat set |xargs -I{} echo -n \{{},\})}

With eval we ask the bash do brace expansion again:

$ eval echo {$(cat set |xargs -I{} echo -n \{{},\})}
{abc} {ab} {ac} {a} {bc} {b} {c} {}

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "security_97": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.