Generate a string of alphanumeric characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate a string of alphanumeric characters
# 8  
Old 03-22-2019
Well, better let's look at the example.
First, let's make the sequence of 62 characters.
Code:
echo $'\n'{{0..9},{A..Z},{a..z}}

Now we have 62 characters per line (Excluding the first line with the character $'\n').
Let's see how many combinations of 2 characters are possible.
Code:
echo $'\n'{{0..9},{A..Z},{a..z}}{{0..9},{A..Z},{a..z}} | wc -l

Subtract 1 ($'\n') from the resulting value, and we get the number 3844 variants.
Add chili pepper to three and get...
Code:
time echo $'\n'{{0..9},{A..Z},{a..z}}{{0..9},{A..Z},{a..z}}{{0..9},{A..Z},{a..z}} | wc -l
> 238328

And the time spent by my laptop will be
Code:
real    0m0,983s

And the last option of the four elements. Prevent you from even running this command!
Code:
time echo $'\n'{{0..9},{A..Z},{a..z}}{{0..9},{A..Z},{a..z}}{{0..9},{A..Z},{a..z}}{{0..9},{A..Z},{a..z}} | wc -l
>14776336 #minus 1
real    2m49,353s

for the time being the command almost paralyzed my laptop.
And this is only a combination of 4 items from 62
Thanks for attention.

Last edited by nezabudka; 03-22-2019 at 06:28 AM..
# 9  
Old 03-22-2019
May i add a few corrections to your estimations? Not, that these will affect the practicality of the outcome in any way, but....

Quote:
all possible combinations of alphanumberica characters of length 12 such that each string will contain numbers and either small or capital letters.
From the wording it follows that we work case-sensitive so that we have a base set of 62 characters and want to find the the 12-combinations.

It also follows that we have to subtract from this the number of all-digit 12-combinations (if basing on multisets, see below) and the number of all-character 12-combinations. It has to be clarified if we still talk combinations without duplicities (as i do assume), that is, 12-tuples of different elements of the base set, or combinations with duplicities.

Lets us for the moment neglect the restricting task and calculate what i'd call the number of the "base set":

If we use combinations without duplicities, that is 12-tuples of different elements of the 62-element set we get n! / ( (n-k)! k! ) or "n over k", the "binomial coefficient", combinations, which is (62! / ( 50! * 12! )) approximately 10^^85 / 10^^64 * 10^^9 ~ 10^^12.

If we use combinations with duplicities (or, basically, a "set of multisets") we need to take into account that an identical element in two different places can be exchanged without changing the combination and hence we get (n+k-1)! / ((n-1)! * k!). 73! / (61! * 12!) is approximately 10^^105 / 10^^83 * 10^^9 ~ 10^^13. The notation for this number is similar to the binomial coefficient ("n over k") but with double braces, i don't know its english name.

We need to reduce the first possible result now by the number of all-character permutations (50 over 12, ~10^^11) in the first case and the second result by this plus the number of all-digit combinations ( multiset 10 over 12, ~10^^7). Both operations won't change the order of magnitude of the results so we get ~10^^12 or ~10^^13 as the respective cardinality of the result sets.

(I do not have any hope this will really help anybody but it was fun nevertheless.)

bakunin

Last edited by bakunin; 03-22-2019 at 09:13 AM..
These 2 Users Gave Thanks to bakunin For This Post:
# 10  
Old 03-22-2019
Thanks @bacunin, very helpful. I myself recently
wrote a program to rearrange the elements.
It can be slightly modified and used to generate just the case as you described.
That a series would consist only of unique values. Just in case,
I will give here my program without changes.
It sorts the represented string first argument by dictionary order,
creates all possible combinations without duplicate elements.
Selects a line from the middle of the list and displays it on the screen.
Nothing prevents to remake the program for outputting all
intermediate lines with permutation as in the second program.
With the input data from 10 elements this program worked on my laptop real 5m30,979s
Code:
#!/bin/bash
: << SPRAVKA
name test.sh version bash 4.4 (x86_64 rhel)
usage: ./test.sh OPAZWYBCN
"od -An -t x2 -w26" can be replaced by "xxd -ps -c26" or hexdump -e '"%02X"'
SPRAVKA
middle() {
        data=$(echo -n "$1" | od -An -t x2 -w26 | sed 's/ \|00//g;s/../a[n++]=\U&;/g')
        bc <<<'define prt(a[],n) { auto i
        for(i=0; i<n; i++) print a[i]
                print "\n"
        }
        define perm(a[], s, n) { auto tmp, i
        if(s == n-1){ o=prt(a[], n)} else {
                for( i=s; i < n; i++) {
                        tmp=a[i]
                        a[i] = a[s]
                        a[s] = tmp
                        o=perm(a[], s+1, n)
                        a[s] = a[i]
                        a[i] = tmp
                }
        }
        }
        ibase=16; obase=10
        '"$data"'
        o=perm(a[], 0, n)'
}
echo -e "$(middle "$1" | sed -r 's/\w{2}/\\x&/g')" | sort |
        head -$(bc <<<'n='"${#1}"'; s=1; for(i=1;i<=n;i++) s*=i; s/2') | tail -1

Code:
#!/bin/bash

middle() {
        bc <<<'define prt(a[],n) { auto i
        for(i=0; i<n; i++) print a[i]
                print "\n"
        }
        define perm(a[], s, n) { auto tmp, i
        if(s == n-1){ o=prt(a[], n)} else {
                for( i=s; i < n; i++) {
                        tmp=a[i]
                        a[i] = a[s]
                        a[s] = tmp
                        o=perm(a[], s+1, n)
                        a[s] = a[i]
                        a[i] = tmp
                }
        }
        }
        a[0]=1; a[1]=2; a[2]=3; a[3]=4; n=4
        o=perm(a[], 0, n)'
}
middle

I do not think that it will interfere. thanks

Last edited by nezabudka; 03-22-2019 at 11:05 AM..
# 11  
Old 03-22-2019
Thanks for the update, Don. I was using an old version (32bit) cygwin on an old PC. bc gave me a range error. However, 11 vs 12 "z's" is my mistake. That may be why bash was able to do the calculation.
# 12  
Old 03-22-2019
Or maybe do you need an algorithm for randomly selecting 12 characters from 62 ?
Code:
shuf -e {{0..9},{A..Z},{a..z}} -zn12


Last edited by nezabudka; 03-22-2019 at 11:51 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to compare numbers in alphanumeric string

Hi, I will be having file names like below, 1420SP1.01804 1420SP1.01805D 1420SP1.01805 1420SP1.01806D 1420SP1.01806 1420SP1.01901D 1420SP1.01901 1420SP1.01902D 1420SP1.01902 1420SP1.01903D 1420SP1.01903 1420SP1.01904 1420SP1.01905 From this, I need to list file names which is... (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. Shell Programming and Scripting

how to insert space in alphanumeric string

Hi everyone, I want help to insert space between digits and letters in a alphanumeric string. INPUT TRY234TER PHY1TYR EXPECTED OUTPUT TRY 234 TER PHY 1 TYR The lines always begin with the letters and the alphabets will be a three letter combination before and after the number. The... (2 Replies)
Discussion started by: kaav06
2 Replies

3. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

4. Shell Programming and Scripting

Sed or trim to remove non alphanumeric and alpha characters?

Hi All, I am new to Unix and trying to run some scripting on a linux box. I am trying to remove the non alphanumeric characters and alpha characters from the following line. <measResults>883250 869.898 86432.4 809875.22 804609 60023 59715 </measResults> Desired output is: 883250... (6 Replies)
Discussion started by: jackma
6 Replies

5. Shell Programming and Scripting

get rid of non-alphanumeric characters

Hi! Could anyone so kindly help me a code to eliminate from a txt file, obtained by collecting and merge several web-page, every word (string) containing non alphabetical, numeric and punctuation character (i.e NON a-zA-Z0-9, underscore and punctuation mark)? Thanks a lot for the help to... (5 Replies)
Discussion started by: mjomba
5 Replies

6. Shell Programming and Scripting

Sorting with non- and alphanumeric characters

Hi guys, I'm new to this forum and I'm not a UNIX expert. I can't figure out this certain problem i'm having: I need to sort some words, some of the words are annotations (enclosed within < and >). I need to have them sorted alphabetically with all non-alphanumeric characters up front. For... (2 Replies)
Discussion started by: fed.m.ang
2 Replies

7. Shell Programming and Scripting

ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well :) I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk). Basically I want to give the user an option to enter some text which will go down as a field within a flat... (3 Replies)
Discussion started by: tugger
3 Replies

8. Shell Programming and Scripting

With Regex Spliting the string into Alphanumeric and Numeric part

Hi there With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part. dummy_postcode_1 = 'SL1' --> res_alpha = 'SL' and res_numeric = '1' dummy_postcode_2 = 'S053' --> res_alpha = 'S' and res_numeric = '053' ... (1 Reply)
Discussion started by: ozgurgul
1 Replies

9. UNIX for Dummies Questions & Answers

AlphaNumeric String Operations

Hi :) I am writing a ksh I have a string of general format A12B3456CD78 the string is of variable length the string always ends with numbers (here it is 78.. it can be any number of digits may be 789 or just 7) before these ending numbers are alphabets (here it is CD can even be... (3 Replies)
Discussion started by: lakshmikanth
3 Replies

10. Shell Programming and Scripting

matching alphanumeric string

how to match an alphanumeric string like the following. i have to do like the following. if the input line is the data is {clock_91b} i have to replace that with the string was ("clock_91b") i tried like $line =~ s/the data is\s+\{(+)\}/the string was \(\"$1\"\)/ which... (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question