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
# 15  
Old 01-09-2019
Quote:
Originally Posted by nezabudka
Probably there should replace the heavyweight utility "xargs" and wrap everything in a function
Very good idea. Since the following works not only with integers but also with arbitrary sets of characters we don't even need a recursion:

Code:
perm ()
{
myset="$1"
n="$2"
buf=""
i=1

while (( i <= n )) ; do
     buf="${buf}${myset}"
     (( i++ ))
done

for x in $buf ; do echo $x ; done

return 0
}

# example calls:
perm "{1,2}" 3

perm "{a,b}" 2

bakunin

/PS: on second thoughts: this is not the permutations but the combinations of the given set, no?

Last edited by bakunin; 01-09-2019 at 04:25 PM.. Reason: added thought
This User Gave Thanks to bakunin For This Post:
# 16  
Old 01-09-2019
Can this be any simpler?
Single statement, longhand, OSX 10.14.1, default bash shell...
Code:
Last login: Wed Jan  9 23:14:09 on ttys000
AMIGA:amiga~> echo $'\n'{a..e}$' '{a..e}$' '{a..e}

a a a 
a a b 
a a c 
a a d 
a a e 
a b a 
a b b 
a b c 
. . . 
e d d 
e d e 
e e a 
e e b 
e e c 
e e d 
e e e
AMIGA:amiga~> _

This User Gave Thanks to wisecracker For This Post:
# 17  
Old 01-09-2019
Hi wisecracker and cjnwl,
Note that if you change:
Code:
echo $'\n'{a..e}$' '{a..e}$' '{a..e}

to:
Code:
printf '%s\n' {a..e}' '{a..e}' '{a..e}

you get rid of the (probably undesired) initial empty line in the output. And to create the desired array of combinations using either a recent bash or ksh93, one could then use:
Code:
OIFS="$IFS"
IFS=$'\n'
array=( $(printf '%s\n' {a..e}' '{a..e}' '{a..e}) )  
IFS="${OIFS}"

And, to verify that it worked, one could then run:
Code:
echo "${#array[@]} '${array[0]}' ... '${array[124]}'"

producing the output:
Code:
125 'a a a' ... 'e e e'

which shows that it is likely that the array named array contains the desired 125 elements.
These 3 Users Gave Thanks to Don Cragun For This Post:
# 18  
Old 01-10-2019
Quote:
Originally Posted by wisecracker
Can this be any simpler?
Single statement, longhand, OSX 10.14.1, default bash shell...
Code:
Last login: Wed Jan  9 23:14:09 on ttys000
AMIGA:amiga~> echo $'\n'{a..e}$' '{a..e}$' '{a..e}
AMIGA:amiga~> _

It's really very short!
Code:
eval echo "$'\n'{$var1..$var2}$' '{$var1..$var2}"

# 19  
Old 01-10-2019
Quote:
Originally Posted by Scrutinizer
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)
  }
  '

What I'm writing now is important for programs with a lot more code, but I think it has benefits in small (awk) programs in this forum too.

Descriptive code

For me - and probably for others like the partly beginner level thread starters more alike - non-descriptive variable names makes it harder to read the code. I see this here in this forum a lot. I assume brevity of code is some kind of goal involved here.

Writing Code that is easily understandable with little effort is far more important to me. And of course readabilty / maintainibility on one side and efficient, clean and short code on the other side are both important and for both must be found a suitable balance. In real life, other people read my code to, or I myself after a long time again and I'd like to avoid that experience I had in the past: "Oh crap! What did I smoke when I wrote that code?"

As an awk example(Scrutinizers variant above is written much better than the first one): split(a,b,c) vs split(text,result,pattern)

This helps me even if I do not know the syntax of split, so I do not have to check the documentation for split() right away.

So I would like it to have code, I can easily understand. I like a solution more, which I may find in this forum even years after it was posted and I quickly can understand it.

Global Variables

(actually i decided to use introduces a new global variable in my fix) I think it's good design, to avoid global variables whenever possible(See http://wiki.c2.com/?GlobalVariablesAreBad). For this forum, a solution that does this without globals avoids copy-and-paste problems: "Huu, I take this function, and put it into my code, and I like it, if it just works." and bamm! Variable collision and it crashes or just does not work. Maybe this(not-working-code with side-effects) is a nice to have too in terms of: Make sure you really understand your code, before trying to run it! Do not just copy it! ...but for I appreciate the other way.

Last edited by stomp; 01-10-2019 at 11:21 AM..
# 20  
Old 01-10-2019
Thanks Guys, that's exactly what I was looking for

--- Post updated at 12:27 PM ---

Note that the various shell scripts don't reply to the question, which was first of all for awk (as this is part of a much larger script that I've been given to modify), but more importantly, hard-coded the number of elements.
# 21  
Old 01-10-2019
First of all, i am glad your problem was solved. There are still some points you might want to consider:

Quote:
Originally Posted by cjnwl
Note that the various shell scripts don't reply to the question, which was first of all for awk
ahem - no! You said (#1):
Quote:
I'm trying to write an awk function
Just because you tried we do not have to try the same - after all, you didn't (at that time) have success with what you tried. We often have questions like how can i do X with tool Y where Y is something we positively know to be not suited at all for achieving X. (You wouldn't believe how often i have been asked something analogous to "please explain how i can solder this to that using a refrigerator and the collected works of Shakespeare".) Now, i admit this is not the case with your question but if you need the solution to be in awk, then please say so.

Quote:
Originally Posted by cjnwl
(as this is part of a much larger script that I've been given to modify)
Like this, which is a valid reason to stick with awk. But since you didn't tell us you can hardly blame us for not knowing this, no?

Quote:
Originally Posted by cjnwl
but more importantly, hard-coded the number of elements.
Two things: first, yes, in some cases, but these were merely meant as demonstrations for using a certain method - in this case a certain little-known form of parameter expansion - and it was obvious how to adapt that for an set and any number of elements in the combination set. Addition is like 1 + 1 = 2 doesn't insinuate that i think "2 + 2" to work any different and of course i could say: addition is a commutative and associative operation on either sets of scalar elements or vector spaces .... Somehow, i noticed, though, that many people prefer the first over the second.

Second: in case of my function (#15) this isn't even true, because you can use any set and any number of elements from the set to build combinations. For instance this invocation:

Code:
perm ()
{
myset="$1"
n="$2"
buf=""
i=1

while (( i <= n )) ; do
     buf="${buf}${myset}"
     (( i++ ))
done

for x in $buf ; do echo $x ; done

return 0
}

perm "{1,a,2,b}" 5

will give you 1024 lines from "1111" to "bbbb". You could even call the function (i tried only in ksh93 but suppose it to work in bash too) with arbitrary strings like this:

Code:
# perm "{"ab","XY"}" 3
ababab
ababXY
abXYab
abXYXY
XYabab
XYabXY
XYXYab
XYXYXY


Quote:
Originally Posted by stomp
What I'm writing now is important for programs with a lot more code, but I think it has benefits in small (awk) programs in this forum too.

Descriptive code

For me - and probably for others like the partly beginner level thread starters more alike - non-descriptive variable names makes it harder to read the code[...]
You definitely have a point there. As i am about to post a larger software project for the community within the next days i suggest we should have a discussion about coding standards in general. Perhaps we could all profit somewhat from such a discussion and by seeing what others do and why.

bakunin

Last edited by bakunin; 01-10-2019 at 09:57 AM..
These 2 Users Gave Thanks to bakunin 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