Skip to content

Instantly share code, notes, and snippets.

@DGaffney
Created May 12, 2011 21:38
Show Gist options
  • Select an option

  • Save DGaffney/969514 to your computer and use it in GitHub Desktop.

Select an option

Save DGaffney/969514 to your computer and use it in GitHub Desktop.
## gen.rb
def all_combinations(array)
permutations = []
array.length.downto(2) do |length|
array.permutation(length).each do |perm|
permutations << perm.sort if !permutations.include?(perm.sort)
end
end
return permutations
end
def comm_partial(files, index=0, text="")
if files.length > 2
file = files[index]
if files.last==file
text+="<(cat #{file})"
elsif files.first==file
text+="<(cat #{files[index]}) #{comm_partial(files, index+1, text)})"
else
text+="<(comm -12 <(cat #{files[index]}) #{comm_partial(files, index+1, text)})"
end
if files.first==file
return "comm -12 "+text.chop
else
return text
end
else
return "comm -12 #{files[0]} #{files[1]}"
end
end
files = ['one','two','three','four']
all_combos = all_combinations(files)
shell_script = File.open("script.sh", "w")
shell_script.write("#!/bin/sh")
all_combos.each do |combo|
files = combo.collect{|file| "#{file}.txt"}
function = comm_partial(files)+">>#{combo.join("_")}.txt\n"
shell_script.write(function)
end
shell_script.close
`chmod +x script.sh`
## one.txt
1
4
8
0
## two.txt
1
3
5
7
9
## three.txt
1
2
4
6
8
0
## four.txt
1
2
3
4
5
6
7
8
9
10
##resulting script.sh will look like:
#!/bin/sh
comm -12 <(cat four.txt) <(comm -12 <(cat one.txt) <(comm -12 <(cat three.txt) <(cat two.txt)))>>four_one_three_two.txt
comm -12 <(cat one.txt) <(comm -12 <(cat three.txt) <(cat two.txt))>>one_three_two.txt
comm -12 <(cat four.txt) <(comm -12 <(cat one.txt) <(cat two.txt))>>four_one_two.txt
comm -12 <(cat four.txt) <(comm -12 <(cat one.txt) <(cat three.txt))>>four_one_three.txt
comm -12 <(cat four.txt) <(comm -12 <(cat three.txt) <(cat two.txt))>>four_three_two.txt
comm -12 one.txt two.txt>>one_two.txt
comm -12 one.txt three.txt>>one_three.txt
comm -12 four.txt one.txt>>four_one.txt
comm -12 three.txt two.txt>>three_two.txt
comm -12 four.txt two.txt>>four_two.txt
comm -12 four.txt three.txt>>four_three.txt
#If you run THIS command in terminal, everything is great, and you find that the similarity across all the files is the line where it says "1":
comm -12 <(cat one.txt) <(comm -12 <(cat two.txt) <(comm -12 <(cat three.txt) <(cat four.txt)))
#BUT if you run this in ruby, you get this:
`comm -12 <(cat one.txt) <(comm -12 <(cat two.txt) <(comm -12 <(cat three.txt) <(cat four.txt)))`
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `comm -12 <(cat one.txt) <(comm -12 <(cat two.txt) <(comm -12 <(cat three.txt) <(cat four.txt)))'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment