How to generate all combinations of group lists at the same time?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to generate all combinations of group lists at the same time?
# 8  
Old 04-16-2017
Quote:
Originally Posted by nengcheng
Thanks for your advice, I understand what you mean. Frankly speaking, I majoring biology and need to analyze some datasets. If I used the scripts in my paper, maybe I could list your name as co-author or in the acknowledgement.
Thanks for the offer, but don't bother. We're here to offer help and learn together. And seeing someone, e.g. a biologist, improve her/his coding skills is one of the rewards we take away. This was the meaning of my remark, and certainly Don Cragun's as well.
These 2 Users Gave Thanks to RudiC For This Post:
# 9  
Old 04-22-2017
Hi.

At our shop we often like to produce generalized solutions. For the case of combinations and permutations, a perl module:
Code:
Math::Combinatorics can produce:

      use Math::Combinatorics;
      my @n = qw(a b c);
      print join("\n", map { join " ", @$_ } combine(2,@n)),"\n";

    Output:

      a b
      a c
      b c

For any set of strings that you get into array n.

That allows easy parameterization of generating combinations. Of course, the infrastructure code takes up some room, so utilities can grow to a hundred lines. For many this would not be worth the effort, but consider how easy it is to produce something like the demonstration script:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate combinations from CL, file.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C combination permutation my-columns dixf

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, read from command line, combinations of 2:"
combination -2 a b c d

pl " Results, read from command line, combinations of 3:"
combination -3 a b c d

pl " Results, read from file, columnized:"
combination -2 < $FILE |
my-columns

pl " Results, read from file, columnized, better separation:"
combination -2 < $FILE |
my-columns -s='/ / | /'

pl " Results, read from command line, but permutations, columnized:"
permutation a b c d |
my-columns -s='/ / | /'

pl " Details of combination:"
dixf combination

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
combination (local) 1.6
permutation (local) 1.1
my-columns (local) 1.6
dixf (local) 1.42

-----
 Input data file data1:
a b c d 
E F G H

-----
 Results, read from command line, combinations of 2:
b c
b d
b a
c d
c a
d a


-----
 Results, read from command line, combinations of 3:
a c d
a c b
a d b
c d b


-----
 Results, read from file, columnized:
a b a d a F a H b d b F b H c E c G d E d G E F E H F H
a c a E a G b c b E b G c d c F c H d F d H E G F G G H

-----
 Results, read from file, columnized, better separation:
a b | a E | a H | b E | b H | c F | d E | d H | E H | G H
a c | a F | b c | b F | c d | c G | d F | E F | F G
a d | a G | b d | b G | c E | c H | d G | E G | F H

-----
 Results, read from command line, but permutations, columnized:
a b c d | a c d b | b a c d | b c d a | c a b d | c b d a | d a b c | d b c a
a b d c | a d b c | b a d c | b d a c | c a d b | c d a b | d a c b | d c a b
a c b d | a d c b | b c a d | b d c a | c b a d | c d b a | d b a c | d c b a

-----
Details of combination:
Code:
combination     Produce n combinations of arguments or file: [-n] a1 a2 ... am (what)
Path    : ~/bin/combination
Version : 1.6
Length  : 172 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Help    : probably available with -h,--help
Modules : (for perl codes)
 warnings       1.23
 strict 1.08
 English        1.09
 Carp   1.3301
 Data::Dumper   2.151_01
 Getopt::Long   2.42
 feature        1.36_01
 Math::Combinatorics    0.09

We currently don't post our local codes, but we provide some details on what is done, and how it is accomplished.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process 2 lists at the same time and move files.

I have this while loop, that works but is quite slow to process though. I'm hopping there might be a faster/better way to find what I'm looking for. I have 2 lists of numbers, and want to only find files where a file name has both values present. each list has about 100 values. while... (10 Replies)
Discussion started by: whegra
10 Replies

2. Shell Programming and Scripting

• Write a shell script that upon invocation shows the time and date and lists all the logged-in user

help me (1 Reply)
Discussion started by: sonu pandey
1 Replies

3. Shell Programming and Scripting

All possible combinations

Hi, I have an input file like this a b c d I want to print all possible combinations between these records in the following way aVSb aVSc aVSd bVSc bVSd cVSd VS indicates versus. All thoughts are appreciated. (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Generate a Execution time reports using log files.

Hi Experts, I am having a requirement, where i need to generate a report of the execution time of all the processes. All the processes generate the log files in a log directory and I can get the execution time from the log files. like below is the log file. /home/vikas/log >ls -l... (2 Replies)
Discussion started by: k_vikash
2 Replies

5. Shell Programming and Scripting

How to make diff show differences one line at a time and not group them?

Is there a way to tell diff to show differences one line at a time and not to group them? For example, I have two files: file1: line 1 line 2 line 3 diff line 4 diff line 5 diff line 6 line 7 file2: line 1 line 2 line 3 diff. line 4 diff. line 5 diff. line 6 line 7 (13 Replies)
Discussion started by: mmr11408
13 Replies

6. AIX

Want to find the last access time for large group of files at one go

Dear All, I'm working as a DBA and dont have much knowledge at OS level commands.we have requirement that we need find the files which has been last accessed >= apr 2010and also access date <= apr 2010 for a large set of files.Do know some commands likeistat, ls -u.But can anyone provide me the... (4 Replies)
Discussion started by: dbanrb
4 Replies

7. Shell Programming and Scripting

Generate array name at run time Korn shell

Hi, I am trying to define number of array based on constant derived during execution phase of a script. Here is what i am trying.. #First Part, Get LUN input from User lun_count=4 count=0 set -A my_lun while : do while ]; do read L?"Enter Lun "$count" Number:" ... (2 Replies)
Discussion started by: harris2107
2 Replies

8. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

9. UNIX for Advanced & Expert Users

Combinations

Hello All, i have two files, one of the format A 123 B 124 C 234 D 345 And the other A 678 B 789 C 689 D 567 I would like to combine them into one file with three columns: A 123 678 B 124 789 C 234 689 (4 Replies)
Discussion started by: Khoomfire
4 Replies
Login or Register to Ask a Question