awk - function to return permutations of n items out of m


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - function to return permutations of n items out of m
# 1  
Old 01-09-2019
awk - function to return permutations of n items out of m

Hi,

I'm trying to write an awk function that returns all possible permutations of n items chosen in a list of m items. For example, given the input "a,b,c,d,e" and 3, the function should return the following :
Code:
a a a
a a b
a a c
a b a
a b b
...
c a a
c a b
...
e e c
e e d
e e e

(125 lines = 5 ^^ 3).

I've managed to write a function with nested loops that works, but only for a predetermined depth. I can't seem to manage the recursion if I want n to be variable.

Also, an added difficulty is that I need the results in an array for further processing in the same script, not just output to the console.

Any help would be much appreciated.
Chris
# 2  
Old 01-09-2019
Quote:
Originally Posted by cjnwl

....
I've managed to write a function with nested loops that works, but only for a predetermined depth.
...
Then. please post your function and script(s) what you wrote so we can help you.
# 3  
Old 01-09-2019
Thanks for your prompt response. I didn't post my code as it's not really much use. I've also searched this forum, but none of the threads about combinations and/or permutations does exactly what I need.

Here's what I have so far. It's not in a function as yet, and the loops are hard-coded two deep. The variable len isn't used at all.
Code:
BEGIN {
  list = "a,b,c,d,e"
  len = 3
  sep = ","
  n = split(list,words,sep)
#  for (i in words) print i, words[i]
  for (i=1;i<=n;i++) {
    for (j=1;j<=n;j++) {
	  print words[i], words[j]
	}
  }
}

Ultimately, I need to be able to access and manipulate the results in an array.

Thanks,
Chris
# 4  
Old 01-09-2019
Some recursion will help...

Code:
#!/usr/bin/awk -f

function perm(list,len,current_set,      word) {
  if(len==1)
    for(word in list)
      print current_set list[word]
  else
    for(word in list)
      perm(list,len-1,current_set list[word] " ")
}

BEGIN {
  list = "a,b,c,d,e"
  sep = ","
  n = split(list,words,sep)
  perm(words,4)
}


Last edited by stomp; 01-10-2019 at 08:59 AM.. Reason: fixed space at the beginning, removed unnecessary braces {..}, removed depth var
This User Gave Thanks to stomp For This Post:
# 5  
Old 01-09-2019
Another variation:

Code:
awk -v n=3 -v inp="a,b,c,d,e" '
  function perm(p,s,	i) {
    for(i=1;i<=n;i++)
      if(p==1)
        printf "%s%s\n",s,A[i]
      else
        perm(p-1,s A[i]" ")
  }

  BEGIN {
    split(inp,A,/,/)
    perm(n)
  }
'

# 6  
Old 01-09-2019
@Scrutinizer:
... a small fix (your variant does only use n elements of the list, not all)

Code:
awk -v n=3 -v inp="a,b,c,d,e" '
  function perm(p,s,    i) {
   for(i=1;i<=c;i++)
     if(p==1)
       printf "%s%s\n",s,A[i]
      else
        perm(p-1,s A[i]" ")
  }
 
  BEGIN {
    c=split(inp,A,/,/)
    perm(n)
  }
'


Last edited by stomp; 01-10-2019 at 04:52 AM..
This User Gave Thanks to stomp For This Post:
# 7  
Old 01-09-2019
I've modified stomp's suggestion to make it bit more flexible:
Code:
function perm(list,len,current_word,depth,      word) {

        # print "len=" len " depth=" depth " current_word=" current_word
        if(len==1) {
                for(word in list) {
                        print current_word " " list[word]
                }
        } else {
                for(word in list) {
                        perm(list,len-1,current_word " " list[word],depth+1)
                }

        }
}

BEGIN {
  list=(!list)? "a,b,c,d,e" : list
  len=(!len) ? 3 : len
  sep = ","
  split(list,words,sep)
  perm(words,len)
}

Sample invocations:
Code:
awk -f permute.awk </dev/null
awk -v list='a,b,c,d' -v len=2  -f permute.awk </dev/null
awk -v list='a,b,c,d' -v len=3  -f permute.awk </dev/null
awk -v list='a,b,c,d,e' -v len=3  -f permute.awk </dev/null


Last edited by vgersh99; 01-09-2019 at 02:11 PM..
This User Gave Thanks to vgersh99 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

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

awk permutations and combinations

hello, I'm reading this thread, in which there is this code :awk ' function comb(v,i) { for(i in A) { delete A; if(length(A)) comb((v?v"+":x)i) else print v"+"i A; } } { A } END { comb(); } ' infilebut I can't understand where does v come... (5 Replies)
Discussion started by: daPeach
5 Replies

4. UNIX for Dummies Questions & Answers

awk colon separated items

Hi, I need to filter my data based on items in column 23. Column 1 until column 23 are tab separated. This is how column 23 looks like: PRIMARY=<0/1:504:499,5:.:.:.:0.01:1:15:.> I want to extract lines if items 7 (separated by : ) in column 23 are more than 0.25 . In example above , item... (2 Replies)
Discussion started by: janshamsani
2 Replies

5. Shell Programming and Scripting

Permutations with awk

Hello I have a very simple input file in which there are a list of numbers: 1 2 3 4 5 6 7 8 9 10 My actual dataset is about 200 lines long. I was wondering how to add different permutations of 3 numbers for all the numbers in the dataset. For example: 1+2+3; 3+5+7; 2+8+1; 9+3+4... (1 Reply)
Discussion started by: Rabu
1 Replies

6. Shell Programming and Scripting

awk, help me - counting items and listing them

This is my first ever post... please help! :o I have two columns....here is part of the file... 12, 46798 6692, 46799 5710, ... (3 Replies)
Discussion started by: pelhabuan
3 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies

9. Shell Programming and Scripting

Function's return value used inside awk

I have a file with the record of person: cat > $TMP/record.txt John Torres M Single 102353 Address Mark Santos M Maried 103001 Address Carla Maria F Maried 125653 Address #!/bin/ksh ManipulateID(){ ... return 0; ... #or return 1; } cat $TMP/record.txt | awk 'BEGIN {printf... (4 Replies)
Discussion started by: Orbix
4 Replies

10. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies
Login or Register to Ask a Question