Generating all possible combinations of values in field 1 (awk)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Generating all possible combinations of values in field 1 (awk)
# 1  
Old 06-11-2014
Generating all possible combinations of values in field 1 (awk)

Input:
Code:
A|1
B|2
C|3
D|4

Output:
Code:
A+B|3
A+C|4
A+D|5
B+C|5
B+D|6
C+D|7
A+B+C|6
A+B+D|7
A+C+D|8
B+C+D|9
A+B+C+D|10

I only managed to get the output for pairs of $1 values (i.e. combination of length 2):
Code:
A+C|4
A+D|5
B+C|5
B+D|6
C+D|7

using this code:
Code:
gawk '
BEGIN{FS=OFS="|"}
{
   array_1[++a]=$1
   array_2[++b]=$2
}
END{
   for(i=1; i<=NR; i++){
      for(j=1; j<=(NR-i); j++){
         print array_1[i] "+" array_1[(i+j)] "|" array_2[i] + array_2[i+j]
      }
   }
}' input

How could I managed to add more than 2 values (i.e. the entire powerset)???

Thanks for your help !

Last edited by beca123456; 06-12-2014 at 02:07 AM..
# 2  
Old 06-12-2014
Quote:
Originally Posted by beca123456
. . . How could I managed to add more than 2 values (i.e. the entire powerset)???
. . .
... by adding more than 2 values ... You were on the right way. Just extending your code snippet:
Code:
awk     'BEGIN          {FS=OFS="|"}
                        {array_1[++a]=$1
                         array_2[++b]=$2}
         END            {for (i=1; i<=NR; i++) {
                           for (j=i+1; j<=(NR); j++) {
                                print array_1[i] "+" array_1[j] "|" array_2[i] + array_2[j] }}
                         for (i=1; i<=NR; i++) {
                           for (j=i+1; j<=(NR); j++) {
                             for (k=j+1; k<=(NR); k++) {
                                print array_1[i] "+" array_1[j] "+" array_1[k] "|" array_2[i] + array_2[j] + array_2[k] }}}
                         print array_1[1] "+" array_1[2] "+" array_1[3] "+" array_1[4] "|" array_2[1] + array_2[2] + array_2[3] + array_2[4]
                        }
        ' file
A+B|3
A+C|4
A+D|5
B+C|5
B+D|6
C+D|7
A+B+C|6
A+B+D|7
A+C+D|8
B+C+D|9
A+B+C+D|10

# 3  
Old 06-12-2014
Thanks RudiC for your help !

The trick is that my real file contains ~100 records.
The plan was to avoid writing 99 blocks (one for each iteration) !!!Smilie
# 4  
Old 06-13-2014
Ignoring the order
Code:
awk 'func int2bin(num) {
  bin = "";
  for(l = num; l >= 1; l/=2) {
    l = sprintf("%d", l);
    bin = (l%2 bin)}
  return bin}
  BEGIN {FS = OFS = "|"}
  {a[++n] = $1;
  b[n] = $2}
  END {cnt = (2^n - 1);
    for(j = 1; j <= cnt; j++) {
      bin = sprintf("%*d", n, int2bin(j));
      split(bin, t, "");
      out=""; sum="";
      for(k = 1; k <= n; k++) {
        if(t[k] == 1)
          {out = (out == "") ? a[k] : (out "+" a[k]);
          sum += b[k]}}
      print out, sum}}' file

# 5  
Old 06-13-2014
awk:
Code:
awk '{A[$1]=$2; for(i in A)if(!(i~"(^|\\+)" $1 "$")){c=i"+"$1; A[c]=A[i]+$2; print c, A[c]}}' FS=\| OFS=\| file

-- multiline --
Code:
awk '
  {
    A[$1]=$2
    for(i in A) if(!(i~"(^|\\+)" $1 "$")) {
      c=i "+" $1
      A[c]=A[i]+$2
      print c, A[c]
    }
  }
' FS=\| OFS=\| file



--
To produce sorted output, it could be combined with sort:
Code:
awk '
  {
    A[$1]=$2
    for(i in A) if(!(i~"(^|\\+)" $1 "$")) {
      c=i "+" $1
      A[c]=A[i]+$2
      print length(c), c, A[c]
    }
  }
' FS=\| OFS=\| file | sort -nt\| | cut -d\| -f2-


Code:
A+B|3
A+C|4
A+D|5
B+C|5
B+D|6
C+D|7
A+B+C|6
A+B+D|7
A+C+D|8
B+C+D|9
A+B+C+D|10


Last edited by Scrutinizer; 06-13-2014 at 07:03 AM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert field values in a record using awk command

Hi Friends, Below is my input file with "|" (pipe) as filed delimiter: My Input File: HDR|F1|F2||||F6|F7 I want to inser values in the record for field 4 and field 5. Expected output HDR|F1|F2||F4|F5|F6|F7 I am able to append the string to the end of the record, but not in between the... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

2. UNIX for Dummies Questions & Answers

Values with common field in same line with awk

Hi all ! I almost did it but got a small problem. input: cars red cars blue cars green truck black Wanted: cars red-blue-green truck black Attempt: gawk 'BEGIN{FS="\t"}{a = a (a?"-":"")$2; $2=a; print $1 FS $2}' input But I also got the intermediate records... (2 Replies)
Discussion started by: beca123456
2 Replies

3. Shell Programming and Scripting

awk help - matching a field with certail values

Hello there, I have a file with few fields separated by ":". I wrote a below awk to manipulate this file: awk 'BEGIN { FS=OFS=":" }\ NR != 1 && $2 !~ /^98/ && $8 !~ /^6/{print $0}' $in_file > $out_file What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 -... (2 Replies)
Discussion started by: juzz4fun
2 Replies

4. Shell Programming and Scripting

Nawk One Liner to Count Field Combinations

I have text files, fields separated by commas, and there are 115 fields. I need to use nawk to look at each line, if field $4==10, then count how many times the combination of field $5 and field $11 occur in the file. I tried the following: nawk -F, '{if($4==10){tg++;cd++,tgcd++}}END{for(i in... (3 Replies)
Discussion started by: he204035
3 Replies

5. Shell Programming and Scripting

Putting values into order in a field using awk

Hi, I am using UBUNTU 12.04. I have a dataset as follows: Column#1 Column#2 Column#3 .... Column#50 1 154878 1 145145 2 189565 2 454121 ... (5 Replies)
Discussion started by: Homa
5 Replies

6. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

7. Shell Programming and Scripting

AWK: combining consecutive values in a field

Hi, Here is my sample input X 2 AAA Y 3 BBB Y 2 CCC Z 4 DDD In field 1, if the value of one line is same as that of next line, I want to concatenate the corresponding value of the second line in the third field with the value of the third field of first line. And I dont need the third... (2 Replies)
Discussion started by: polsum
2 Replies

8. Shell Programming and Scripting

counting lines containing two column field values with awk

Hello everybody, I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted. This was my first approach to tackle the problem (I'm... (6 Replies)
Discussion started by: origamisven
6 Replies

9. Shell Programming and Scripting

Generating Combinations

Hi, I need to generate all combinations upto n-1 level, if the input file looks like say, A B C D . . .... I need to generate all combinations such that first value remains constant and the remaning are combined with all possible ways. Output A AB AC AD ABC (1 Reply)
Discussion started by: zorg4u
1 Replies

10. UNIX for Dummies Questions & Answers

Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value. BEGIN {FS=","; max=0} NF == 7 {if (max < $6) max = $6;} END { print man, min} where $6 is the column of a field separated by a comma (3 Replies)
Discussion started by: Kirichiko
3 Replies
Login or Register to Ask a Question