Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Arbitrary permutation and combination script

Shell Programming and Scripting


Tags
combination, permutation, shell

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-18-2012
complex.invoke's Unix or Linux Image
complex.invoke complex.invoke is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 30 December 2015, 11:56 PM EST
Location: BeiJing China
Posts: 236
Thanks: 6
Thanked 29 Times in 8 Posts
MySQL Arbitrary permutation and combination script


Code:
#!/bin/bash
# permutation_combination.sh

# Version: 2.0
# Author : YongYe <complex.invoke@gmail.com>

arg0=-1
argv=${3}
number=${2}
eval ary=({1..${1}})
length=${#ary[@]}
percom(){ nsloop i ${1} number${2} ${3} ${4} ${5}; }
invoke(){ echo $(percom ${argu} nsloop -1) prtcom $(percom ${argu}); }
permut(){ echo -n "${1} arg${i} ${2} "; (( ${#} != 0 )) && echo -n " length "; }
combin(){ (( ${#} != 0 )) && echo -n "${1} arg$((i+1)) arg${i} length " || echo -n "arg$((i+1)) "; }
prtcom(){ num=0; for i in ${@}; do echo -n ${ary[${!i}]}; (( ++num != number )) && echo -n .; done; echo; }

nsloop()
{ 
    local arc arg
    arg=${1//arg}
    for((${1}=${2}+1; ${1}<${3}; ++${1})); do 
         if [[ ${1//[0-9]} == arg ]]; then
               for((arc=1; arc!=arg; ++arc)); do
                    (( ${1} == arg${arc} )) && continue 2
               done
         fi
         eval eval \\\$\{{4..${#}}\}
    done
}

case ${3} in
     p) argu="-0 +1 permut" ;;
     c) argu="-1 +0 combin" ;; 
esac

$(invoke)

Invoke the script:
C 4 3
Code:
[root@node2 ~]# bash permutation_combination.sh 4 3 c
1.2.3
1.2.4
1.3.4
2.3.4

P 4 3
Code:
[root@node2 ~]# bash permutation_combination.sh 4 3 p | xargs -n 6
1.2.3 1.2.4 1.3.2 1.3.4 1.4.2 1.4.3
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ke is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 30 December 2015, 11:56 PM EST
Location: BeiJing China
Posts: 236
Thanks: 6
Thanked 29 Times in 8 Posts
Optimized!
It's running faster now!
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Select combination unique using shell script osmanux Shell Programming and Scripting 4 01-09-2012 10:09 PM
Combination of case and If else in shell script Pauline mugisha Shell Programming and Scripting 2 12-01-2009 01:06 AM
Formula to get combination… sum Amit.Sagpariya Shell Programming and Scripting 1 08-31-2009 05:04 AM
How To Analyze This (Cryptography Random?) Permutation...? lucky7 What is on Your Mind? 3 05-26-2009 12:46 PM
Combination backup/VI script cbo0485 Shell Programming and Scripting 3 01-22-2009 04:09 PM



All times are GMT -4. The time now is 02:55 PM.