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,478

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
  • 10,313