Sponsored Content
Top Forums Shell Programming and Scripting awk - function to return permutations of n items out of m Post 303028497 by durden_tyler on Wednesday 9th of January 2019 01:38:08 PM
Old 01-09-2019
A very un-awk-like and more Perl-like script. Smilie

I added the sort routine because array iteration doesn't return the elements in order.
(Definitely not a good idea for large arrays.)

Note that the OP wants the results in an array for further processing in the same script.
I'm not sure how to retrieve the array elements in the order that they were inserted.
Maybe a forum member can suggest how to do so.

Code:
echo |
awk 'function generate_permutations(cs, item, n, k){
         if (k == 0){
             iter = iter + 1;
             arr[iter] = item;
             return;
         }
         for (i in cs){
             new_item = item""cs[i];
             generate_permutations(cs, new_item, n, k-1);
         }
     }
     {
          split("", arr, "");
          iter = 0;
          str = "a,b,c,d,e";
          n = split(str, charset, ",");
          k = 3;
          generate_permutations(charset, "", n, k);
          asort(arr);
          for (i=1; i <= iter; i++){
              printf "%s\n", arr[i];
          }
     }
    '


Last edited by durden_tyler; 01-09-2019 at 02:39 PM.. Reason: Too many blank lines added automatically between paragraphs.
This User Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Template::Iterator(3)					User Contributed Perl Documentation				     Template::Iterator(3)

NAME
Template::Iterator - Data iterator used by the FOREACH directive SYNOPSIS
my $iter = Template::Iterator->new(@data, \%options); DESCRIPTION
The "Template::Iterator" module defines a generic data iterator for use by the "FOREACH" directive. It may be used as the base class for custom iterators. PUBLIC METHODS
new($data) Constructor method. A reference to a list of values is passed as the first parameter. Subsequent calls to get_first() and get_next() calls will return each element from the list. my $iter = Template::Iterator->new([ 'foo', 'bar', 'baz' ]); The constructor will also accept a reference to a hash array and will expand it into a list in which each entry is a hash array containing a '"key"' and '"value"' item, sorted according to the hash keys. my $iter = Template::Iterator->new({ foo => 'Foo Item', bar => 'Bar Item', }); This is equivalent to: my $iter = Template::Iterator->new([ { key => 'bar', value => 'Bar Item' }, { key => 'foo', value => 'Foo Item' }, ]); When passed a single item which is not an array reference, the constructor will automatically create a list containing that single item. my $iter = Template::Iterator->new('foo'); This is equivalent to: my $iter = Template::Iterator->new([ 'foo' ]); Note that a single item which is an object based on a blessed ARRAY references will NOT be treated as an array and will be folded into a list containing that one object reference. my $list = bless [ 'foo', 'bar' ], 'MyListClass'; my $iter = Template::Iterator->new($list); equivalent to: my $iter = Template::Iterator->new([ $list ]); If the object provides an "as_list()" method then the Template::Iterator constructor will call that method to return the list of data. For example: package MyListObject; sub new { my $class = shift; bless [ @_ ], $class; } package main; my $list = MyListObject->new('foo', 'bar'); my $iter = Template::Iterator->new($list); This is then functionally equivalent to: my $iter = Template::Iterator->new([ $list ]); The iterator will return only one item, a reference to the "MyListObject" object, $list. By adding an "as_list()" method to the "MyListObject" class, we can force the "Template::Iterator" constructor to treat the object as a list and use the data contained within. package MyListObject; ... sub as_list { my $self = shift; return $self; } package main; my $list = MyListObject->new('foo', 'bar'); my $iter = Template::Iterator->new($list); The iterator will now return the two items, '"foo"' and '"bar"', which the "MyObjectList" encapsulates. get_first() Returns a "($value, $error)" pair for the first item in the iterator set. The $error returned may be zero or undefined to indicate a valid datum was successfully returned. Returns an error of "STATUS_DONE" if the list is empty. get_next() Returns a "($value, $error)" pair for the next item in the iterator set. Returns an error of "STATUS_DONE" if all items in the list have been visited. get_all() Returns a "(@values, $error)" pair for all remaining items in the iterator set. Returns an error of "STATUS_DONE" if all items in the list have been visited. size() Returns the size of the data set or undef if unknown. max() Returns the maximum index number (i.e. the index of the last element) which is equivalent to size() - 1. index() Returns the current index number which is in the range 0 to max(). count() Returns the current iteration count in the range 1 to size(). This is equivalent to index() + 1. first() Returns a boolean value to indicate if the iterator is currently on the first iteration of the set. last() Returns a boolean value to indicate if the iterator is currently on the last iteration of the set. prev() Returns the previous item in the data set, or "undef" if the iterator is on the first item. next() Returns the next item in the data set or "undef" if the iterator is on the last item. parity() Returns the text string "even" or "odd" to indicate the parity of the current iteration count (starting at 1). This is typically used to create striped zebra tables. <table> [% FOREACH name IN ['Arthur', 'Ford', 'Trillian'] -%] <tr class="[% loop.parity %]"> <td>[% name %]</td> </tr> [% END %] </table> This will produce the following output: <table> <tr class="odd"> <td>Arthur</td> </tr> <tr class="even"> <td>Ford</td> </tr> <tr class="odd"> <td>Trillian</td> </tr> </table> You can then style the "tr.odd" and "tr.even" elements using CSS: tr.odd td { background-color: black; color: white; } tr.even td { background-color: white; color: black; } odd() Returns a boolean (0/1) value to indicate if the current iterator count (starting at 1) is an odd number. In other words, this will return a true value for the first iterator, the third, fifth, and so on. even() Returns a boolean (0/1) value to indicate if the current iterator count (starting at 1) is an even number. In other words, this will return a true value for the second iteration, the fourth, sixth, and so on. AUTHOR
Andy Wardley <abw@wardley.org> <http://wardley.org/> COPYRIGHT
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template perl v5.16.3 2011-12-20 Template::Iterator(3)
All times are GMT -4. The time now is 07:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy