Script to output custom characters in different order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to output custom characters in different order
# 1  
Old 01-29-2012
Script to output custom characters in different order

Need a script to print a set of characters in different combination. What's the key to accomplish this.

e.g charset: "Abcdefghij1" without quotes.
Code:
block 1: "Abcd"
block 2: "efg"
block 3: "hij1"

I need this script to change the order of the characters and print it to stdout
e.g
print out the blocks separately
Code:
"Abcd" "efg" "hij1"
"Acbd" "feg" "h1ji"
"Adbc" "fge" "j1hi"
...

Or is there a better way to accomplish this with something else?

Last edited by radoulov; 01-29-2012 at 06:16 PM.. Reason: Code tags!
# 2  
Old 01-29-2012
Hi.

Here's a perl script that uses a module for the hard parts:
Code:
#!/usr/bin/env perl

# @(#) p2	Demonstrate combinations and permutations.

use Math::Combinatorics;

my @n = qw(a b c);
print "combinations of 2 from: " . join( " ", @n ) . "\n";
print "------------------------" . ( "--" x scalar(@n) ) . "\n";
print join( "\n", map { join " ", @$_ } combine( 2, @n ) ), "\n";
print "\n";
print "permutations of 3 from: " . join( " ", @n ) . "\n";
print "------------------------" . ( "--" x scalar(@n) ) . "\n";
print join( "\n", map { join " ", @$_ } permute(@n) ), "\n";

exit(0);

producing:
Code:
% ./p2
combinations of 2 from: a b c
------------------------------
c a
c b
a b

permutations of 3 from: a b c
------------------------------
a b c
a c b
b a c
b c a
c a b
c b a

See perldoc Math::Combinatorics for details ... cheers, drl
# 3  
Old 01-30-2012
Thanx drl. I'll try to change your code to fit my need. I have no pre-knowledge off working with perl though
# 4  
Old 01-30-2012
Ok, here's a rough, kind of ugly brute force way using arrays with ksh93 on Solaris. Hopefully someone will come up with a more elegant solution. It loops 10 times, jumbling the blocks as the OP defined. If this is not exactly what you were looking for, hopefully you'll get some ideas. :-)
Code:
#!/usr/dt/bin/dtksh

CHARSET="Abcdefghij1"
integer ctr=10  # Number of times to loop.

# Arrays for original blocks
set -A a_block1
set -A a_block2
set -A a_block3

# Arrays for new mixed blocks
set -A na_block1
set -A na_block2
set -A na_block3

# Split CHARSET into the original blocks.
for (( i=0;i<4;i++ ))
do
  a_block1[${i}]=${CHARSET:${i}:1}
done
for (( i=0;i<3;i++ ))
do
  a_block2[${i}]=${CHARSET:${i}+4:1}
done
for (( i=0;i<4;i++ ))
do
  a_block3[${i}]=${CHARSET:${i}+7:1}
done

#  Print out starting blocks.
print "Block 1: \"${a_block1[0]}${a_block1[1]}${a_block1[2]}${a_block1[3]}\""
print "Block 2: \"${a_block2[0]}${a_block2[1]}${a_block2[2]}\""
print "Block 3: \"${a_block3[0]}${a_block3[1]}${a_block3[2]}${a_block3[3]}\""
print

##  Function to generate a random number between 1 and the number passed
##  in inclusive. Sets $random_nbr.
get_random_nbr() {
  if [[ $1 == 0 ]]; then
    return 1
  else
    random_nbr=$(( ($RANDOM % $1 ) +1 ))
  fi
}

#  Mix 'em up.  Jumble each block, copying into the new mixed array. Do it for each
# block.  This should be made into a function but I just repeated it to get done quicker.

for (( k=0;k<${ctr};k++ ))
do
  # For the length of the array...
  for (( i=0;i<${#a_block1[*]};i++ ))
  do
    # Get a random number...
    get_random_nbr ${#a_block1[*]}
    (( j=$random_nbr -1 ))

    # Use the random number as an index into the mixed array and see if
    # it is not null.
    while [[ -n ${na_block1[${j}]} ]]
    do
      # if not null, get another random number until the array location is null.
      get_random_nbr ${#a_block1[*]}
      (( j=$random_nbr -1 ))
    done

    # The character at the index is null so plug in the character
    # from the original array.
    na_block1[${j}]=${a_block1[${i}]}
  done

  for (( i=0;i<${#a_block2[*]};i++ ))
  do
    get_random_nbr ${#a_block2[*]}
    (( j=$random_nbr -1 ))

    while [[ -n ${na_block2[${j}]} ]]
    do
      get_random_nbr ${#a_block2[*]}
      (( j=$random_nbr -1 ))
    done
    na_block2[${j}]=${a_block2[${i}]}
  done

  for (( i=0;i<${#a_block3[*]};i++ ))
  do
    get_random_nbr ${#a_block3[*]}
    (( j=$random_nbr -1 ))

    while [[ -n ${na_block3[${j}]} ]]
    do
      get_random_nbr ${#a_block3[*]}
      (( j=$random_nbr -1 ))
    done
    na_block3[${j}]=${a_block3[${i}]}
  done

  print "\"${na_block1[0]}${na_block1[1]}${na_block1[2]}${na_block1[3]}\" \c"
  print "\"${na_block2[0]}${na_block2[1]}${na_block2[2]}${na_block2[3]}\" \c"
  print "\"${na_block3[0]}${na_block3[1]}${na_block3[2]}${na_block3[3]}\""

  # Clear the new array blocks for the next run.
  set -A na_block1
  set -A na_block2
  set -A na_block3

done

exit 0

Output:
Code:
$ efs
Block 1: "Abcd"
Block 2: "efg"
Block 3: "hij1"

"Adbc" "egf" "h1ji"
"Abcd" "feg" "h1ij"
"cdbA" "feg" "hj1i"
"cdAb" "gfe" "jhi1"
"cdbA" "egf" "ji1h"
"dbcA" "feg" "ij1h"
"Acdb" "egf" "h1ij"
"bdcA" "gef" "h1ji"
"bdcA" "egf" "ji1h"
"bcdA" "gef" "ji1h"
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awking custom output

i have data that can look like this: echo "Master_Item_Service_is_down=0_njava_lang_NoClassDefFoundError=0_njava_lang_OutOfMemoryError=1_nemxCommonAppInitialization__Error_while_initializing=0_nINFO__Stopping_Coyote_HTTP_1_1_on_http_8080=7_nThe_file_or_directory_is_corrupted_and_unreadable=0_n" ... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Custom wget output

The below hides the messy commands of wget #!/bin/bash cd 'C:\Users\cmccabe\Desktop\wget' wget -O getCSV.txt http://172.24.188.113/data/getCSV.csv progressfilt () { local flag=false c count cr=$'\r' nl=$'\n' while IFS='' read -d '' -rn 1 c do if $flag ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

awk - Why does output order change?

I have this END section in an awk script: END { for (j in littlebin) { print (j, int(log(j)/log(2)) , littlebin) lbsum+=littlebin } for (i in bins) ... (6 Replies)
Discussion started by: treesloth
6 Replies

4. Shell Programming and Scripting

How to print the output in correct order?

Hi, while using following awk commend I’m getting confused, The output is not like as the row present in input files, can anyone explain and tell me how to print in the order like in input. value=$(awk 'FNR>1 && NR==FNR{a=$4;next} a{sum+=$4} END {for(i in sum){printf i"\t"sum/2"@@";}}'... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

5. Shell Programming and Scripting

egrep output order

The order of egrep output seems to be as they occur in the file. How do I get the order as requested? For e.g. file contents: AAA EEE GGG egrep 'GGG|AAA|EEE' file gives AAA EEE GGG instead of GGG AAA EEE (2 Replies)
Discussion started by: madhavb
2 Replies

6. Red Hat

Custom output on FIND

I have a file, ENV.doc somewhere in my home directory. I want to know where the file is located in my sub directories using FIND. But, I want to display only the relative path along with the file name. Thanks, (6 Replies)
Discussion started by: ashok.g
6 Replies

7. HP-UX

Output of Custom package scripts to terminal

Hi, I am doing some testing with creation of depots on HP-UX systems (11.11). Want to display some echo statements based on the processing during checkinstall, pre & postinstall scripts on the terminal. The echo statements are getting directed to /var/adm/sw/swagent.log I want to display... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

8. Shell Programming and Scripting

help removing characters for output file in shell script

hi i'm new to shell scripts and have a small problem i am running a batch converter that returns all flash .flv files in a directory and create a png image from each one the problem i have is the $1 variable , its ok on the first call but on the secound call $1.png , i have extra... (1 Reply)
Discussion started by: wingchun22
1 Replies

9. Shell Programming and Scripting

output string in reversing order

If I have string { I_love_shell_scripts} anyone knows how to have output {stpircs_llehs_evol_I} by using shell and perl ?I know in perl, there is reverse() funcation, but can it be done by not using reverse()? (3 Replies)
Discussion started by: ccp
3 Replies

10. UNIX for Dummies Questions & Answers

replacing characters in order to sort

hi, i want to rename all the file names in order so that they can be sorted later. For example, my filenames are like path\1, path\2...path\10, path\11. But when I sort them, it sorts by the first number, so path\1 gets sorted with path\10. I'm guessing the best way to do this is to rename... (5 Replies)
Discussion started by: gammaman
5 Replies
Login or Register to Ask a Question