4

I want to keep only one permutation among a set of strings and count the occurence of each permutation.

To make things more clear, I want to transform (for instance)

ab
acf
ba
cfa
acf

into

2 ab
3 acf

where 2 and 3 are the number of permutations of {a,b} and {a,c,f} respectively.

How would you do it in bash in the straightest possible way?

user123456
  • 2,348
  • 5
  • 31
  • 53
  • 1
    Is there any chance that any characters will be repeated within a single string? e.g. `abbc` – Arronical Dec 06 '16 at 12:56
  • convert each string to a set of chars, or a sorted list of chars, and save that. In Python you could use itertools.permutations() to get all possible permutations (or number thereof). – boardrider Dec 06 '16 at 19:44

1 Answers1

7

Perl to the rescue!

perl -lne '$h{ join "", sort split // }++ }{ print "$h{$_} $_" for keys %h' < input_file
  • -n reads the input line by line
  • -l removes newlines from input, adds newlines to output
  • split // splits the string into characters
  • sort sorts them (hence uniqueness)
  • join "" creates back a single string from the list of characters
  • %h is a hash table, keys are the strings of sorted characters, values are numbers of occurrences: increased (++) on each encounter
  • }{ "Eskimo greeting" - separates the code to run after the input is exhausted
choroba
  • 9,273
  • 1
  • 30
  • 42