5

Bash can generate multiple strings from single, if you use {...,...} syntax. Like here:

$ echo pgdb{200,10{0,1}}
pgdb200 pgdb100 pgdb101

Is there any way to take a list of strings and produce (hopefully shorter) string that, upon processing via bash word expansion will produce original list (not necessarily in original order?

For example, I'd like this tool/algorithm, that given:

  • postgresql
  • mysql
  • postgres
  • miata

would produce (for example): {postgres{ql,},m{iata,ysql}}

I thought about using trie to represent input strings, but can't figure out how to process this trie to build output string.

someone
  • 51
  • 2
  • 1
    Brace expansion is intended for interactive use to reduce the amount of typing you need to do. If you already have the list of names in a form that you could feed to such a tool, just use that list. Reducing it to a minimal brace expansion just hurts readability. – chepner May 13 '15 at 14:00
  • This would require parsing and comparing every character of every word and would be far more hassle than simply using your predetermined list. –  May 13 '15 at 14:09
  • @chepner my usecase is for generating list in one place, and using it in another. Interactively. I can't scp the list easily as the both places are behind different firewalls and nats. – someone May 13 '15 at 14:31
  • @JID: I understand what would be required, but no idea what you mean by "more hassle" - the list is not predetermined at the moment. And I can't simply generate the list in the place where I need to use it. Nor can I *simply* copy it there. – someone May 13 '15 at 14:32
  • Please post the actual problem you are trying to solve, instead of assuming that dynamic generation of a brace expansion is the solution to that problem. – chepner May 13 '15 at 14:34
  • 1
    @someone If you are generating the braces from the words, then surely they have to be predertimed, unless you just want to generate random brace expansions ? –  May 13 '15 at 14:37
  • Well, sure. Predetermined as in: output of some long command, that checks "whatever", and extracts things from there. There are literally dozens of cases where I would use it. Last time was: extract list of removed hostnames from puppet manifests, and then do something on all of these hosts. – someone May 13 '15 at 14:57
  • @someone, don't use scp, use tar+ssh to handle remote copy of any list of files. – lihao May 13 '15 at 18:30
  • Who said anything about files? Guys, really - please stop trying to find out my motives. My question, I was hoping, is rather simple (in terms of understanding). If you don't know such tool, fine. So be it. But please, stay on topic. – someone May 13 '15 at 19:25

1 Answers1

1

use Compress::BraceExpansion;?

pstef
  • 91
  • 7
  • It is interesting approach, but unfortunately the algorithm is not really smart. It cannot handle sensibly cases where given set of strings has both common prefix, and common suffix. – someone May 13 '15 at 15:53