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?
# 1  
Old 04-14-2017
How to generate all combinations of group lists at the same time?

Hi, everyone

I have a group lists like this (more lines are omitted:
Code:
a b c d 
E F G H
....

I want to generate all combinations of two elements in each group line. What I expected is this:
Code:
a b
a c
a d
b c
b d
c d
E F
E G
E H
F G
F H
G H
..

Any programing language is appreciated.
# 2  
Old 04-14-2017
Instead of just saying you're a beginner, why don't you put a little effort into this and try to solve it on your own. If you get stuck, show us what you have tried and explain where you got stuck.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-14-2017
Quote:
Originally Posted by Don Cragun
Instead of just saying you're a beginner, why don't you put a little effort into this and try to solve it on your own. If you get stuck, show us what you have tried and explain where you got stuck.
Ok, I searched Google and tried python:
Code:
import itertools
paralog = ['a','b','c','d']
for L in range(2, len(paralog)-1):
  for subset in itertools.combinations(paralog, L):
    print(subset)

It just give me one group, and I have more than one thousand groups.

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-15-2017 at 02:34 AM.. Reason: Changed ICODE to CODE tags.
# 4  
Old 04-15-2017
My python is non-existent, but I guess what you post just uses an array of constants and doesn't read and loop your input file.
And, I don't think Don Cragun was talking of copying google results into here, but trying to compose something on your own after (hopefully) having learned from the suggestions you received in here.
Try
Code:
awk '{for (i=1; i<=NF; i++) for (j=i+1; j<=NF; j++) print $i, $j}' file
a b
a c
a d
b c
b d
c d
E F
E G
E H
F G
F H
G H

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 04-15-2017
Quote:
Originally Posted by nengcheng
Ok, I searched Google and tried python:
Code:
import itertools
paralog = ['a','b','c','d']
for L in range(2, len(paralog)-1):
  for subset in itertools.combinations(paralog, L):
    print(subset)

It just give me one group, and I have more than one thousand groups.

Run as python combination.py nengcheng.file
Code:
import fileinput
import itertools

for line in fileinput.input():
    fields = line.split()
    for L in range(2, len(fields)-1):
        for subset in itertools.combinations(fields, L):
            print ' '.join(subset)


Run as perl combination.pl nengcheng.file
Code:
while(<>) {
    my @fields = split;
    while(@fields) {
        my $lead = shift @fields;
        for(@fields) {
            print "$lead $_\n";
        }
    }
}

This User Gave Thanks to Aia For This Post:
# 6  
Old 04-15-2017
Quote:
Originally Posted by RudiC
My python is non-existent, but I guess what you post just uses an array of constants and doesn't read and loop your input file.
And, I don't think Don Cragun was talking of copying google results into here, but trying to compose something on your own after (hopefully) having learned from the suggestions you received in here.
Try
Code:
awk '{for (i=1; i<=NF; i++) for (j=i+1; j<=NF; j++) print $i, $j}' file
a b
a c
a d
b c
b d
c d
E F
E G
E H
F G
F H
G H

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.
# 7  
Old 04-16-2017
Code:
$ 
$ cat list_comb.txt
a b c d
E F G H
$ 
$ perl -lne 'sub comb {return if !$#_;
                 print for glob join "", map "{$_}", map {join ",", @$_}([shift],\@_);
                 comb(@_)}
             comb(split)' list_comb.txt
ab
ac
ad
bc
bd
cd
EF
EG
EH
FG
FH
GH
$ 
$


Last edited by durden_tyler; 04-16-2017 at 04:27 AM..
This User Gave Thanks to durden_tyler For This Post:
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