LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2022, 06:20 AM   #1
jamied_uk
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Rep: Reputation: Disabled
how to Generate a specifically formatted string


How do I generate codes / strings with a specific format like

aaaAaaaaa1aa

12 chars long in total
Also with no duplicate chars (if pos)

where 1 is a number a is lower case and A is upper case? im using linux and like to use bash but pearl is just as good, I was trying to use crunch but that is way too long becuase of the aaaa crap that crunch spits out, any ideas?

Last edited by jamied_uk; 10-13-2022 at 06:24 AM.
 
Old 10-13-2022, 06:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
As a start, you can generate the lower-case letters this way (in bash):
Code:
for i in {a..z}; do echo $i; done | shuf | tr -d '\n' | cut -b-10
 
Old 10-13-2022, 08:27 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120

Why?

There are lots of different methods, but which ones are appropriate depends on what these codes are to be used for.

Is that example the specific format you want (4th char uppercase, 10th numeric), or will there be an arbitrary combination of "aA1" which needs parsing, etc?

Are you trying to generate unique ids, or passwords, or something else?


Last edited by boughtonp; 10-13-2022 at 08:29 AM.
 
Old 10-13-2022, 09:14 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,248

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
so you have 5 fields. Generate those fields independently (depending on their requirements) and concatenate the results.
 
Old 10-13-2022, 10:09 AM   #5
teckk
Senior Member
 
Registered: Oct 2004
Distribution: Arch
Posts: 4,448
Blog Entries: 5

Rep: Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548
Quote:
12 chars long in total
Also with no duplicate chars (if pos)
Real simple example without any effort to make it concise.
Code:
a=({a..z})
b=({A..Z})
c=({0..9})

for i in {1..4}; do
    echo -n ${a["$[RANDOM % ${#a[@]}]"]}
    echo -n ${b["$[RANDOM % ${#b[@]}]"]}
    echo -n ${c["$[RANDOM % ${#c[@]}]"]}
done
echo ""
 
2 members found this post helpful.
Old 10-13-2022, 10:37 AM   #6
jamied_uk
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Nice this is exaclty what I was looking for ty your a genius!
 
Old 10-13-2022, 11:20 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Quote:
Originally Posted by jamied_uk View Post
Nice this is exaclty what I was looking for ty your a genius!
It is not exactly what you're looking for, because it allows duplicates which you claimed to want to avoid.

It is easy to prevent duplicates - and can be done in one line instead of nine - but whether it's a suitable approach depends on what you're doing, which is still unstated.


Last edited by boughtonp; 10-13-2022 at 11:21 AM.
 
Old 10-13-2022, 12:35 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
What’s crunch?
 
Old 10-13-2022, 06:21 PM   #9
jamied_uk
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
I made this before i read replies from here and been refining it for a lil while i saw some great answers aswell, heres my final code

https://jnet.forumotion.com/t1886-ka...g-example#2931

As you asked what crunch is its for creating wordlists
Code:
#!/bin/bash
# (c)J~Net 2022
# jnet.club
# https://jnet.forumotion.com/t1887-using-bash-to-generate-keys-in-a-specific-format#2932
#
#
# ./key.sh 1000
#
#  cat keys.txt | grep "jn5W" 
#
echo "Hi $USER, Welcome To Key Gen V5 By J~Net 2022"
echo "Setting Up Pre Reqs!"
# Lets check if you have required apps installed to do this job...
input=`which perl`
if [ $input = "/usr/bin/perl" ]
then
    echo "You Have Perl Installed Already"
else
    echo "Installing Perl"
    sudo apt install -y perl
fi
#
input=`which crunch`
if [ $input = "/usr/bin/crunch" ]
then
    echo "You Have Crunch Installed Already"
else
    echo "Installing Crunch"
    sudo apt install -y crunch
fi
#
if [ -z "$1" ]
then
	echo "How Many keys You Want?"
	read num

else
        num=$1

fi
#
file="wordlists/1.txt"
if [[ -f $file ]]
then
    echo ""
else
    sudo mkdir wordlists
    touch $file
    crunch 3 3 abcdefghijklmnopqrstuvwxyz -d 1 -t @@% | perl -ne 'print unless /([A-Z]).*\1/' > $file
fi
#
file="wordlists/2.txt"
if [[ -f $file ]]
then
    echo ""
else
    sudo mkdir wordlists
    touch $file
    crunch 3 3 abcdefghijklmnopqrstuvwxyz -d 1 -t ,@@ | perl -ne 'print unless /([A-Z]).*\1/' > $file
fi
#
#
file="wordlists/3.txt"
if [[ -f $file ]]
then
    echo ""
else
    sudo mkdir wordlists
    touch $file
    crunch 3 3 abcdefghijklmnopqrstuvwxyz -d 1 -t @@@ | perl -ne 'print unless /([A-Z]).*\1/' > $file
fi
#
file="wordlists/4.txt"
if [[ -f $file ]]
then
    echo ""
else
    sudo mkdir wordlists
    touch $file
    crunch 3 3 abcdefghijklmnopqrstuvwxyz -d 1 -t %@@ | perl -ne 'print unless /([A-Z]).*\1/' > $file
fi
#
i=0
echo "Getting $num Random Wifi keys"
   	
while [ $i -lt $num ]
do
   	echo ""
	i1=`shuf -n 1 wordlists/1.txt`
	i2=`shuf -n 1 wordlists/2.txt`
	i3=`shuf -n 1 wordlists/3.txt`
	i4=`shuf -n 1 wordlists/4.txt`

	echo "$i1$i2$i3$i4" >> keys.txt
	echo "Generating New Wifi Key $i of $num Is: $i1$i2$i3$i4"
	
   ((i++))

done

Last edited by jamied_uk; 10-13-2022 at 07:34 PM.
 
  


Reply

Tags
linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 06:21 AM
[SOLVED] I specifically want to delete my grep output string from the text file tropical781 Linux - Newbie 5 10-09-2012 04:48 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 08:22 PM
generate a password string to be used by the useradd command powah Linux - Security 5 07-18-2007 11:16 PM
how to suppress the newline in the formatted string returned by ctime? thanhvn Programming 1 06-20-2006 09:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration