Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
This routine should solve your string-splitting task. Adapt to your needs:
$ cat go.sh
#!/usr/bin/env bash
str="This will be divided"
ptr=0
interval=3
while [[ $ptr -le ${#str} ]]; do
printf "'%-3s'\n" "${str:$((ptr)):$((interval))}"
ptr=$((ptr+interval))
done
In use:
$ ./go.sh
'Thi'
's w'
'ill'
' be'
' di'
'vid'
'ed '
This uses bash's built in parameter expansion tooling for substring extraction. Given a variable foo:
$foo is replaced with the contents of foo${#foo} is replaced with the number of items in foo. For an array, this is the number of assigned values; for a non-array, this is the length of the string represented by $foo (foo=10; echo ${#foo} yields 2).${foo:4:4} is replaced with the contents of foo, starting at the fifth character (zero is the first index), for four characters.The last of these is the key here: We use our string, a pointer for how many characters we've printed so far, and a defined interval.
We combine this with formated strings: %-3s will output a left-justified, space-padded string. So if we provide only one or two characters, space padding is added to the right for us.
To write to files, you can simply change
printf "'%-3s'\n" "${str:$((ptr)):$((interval))}"
to
fname="$dir/$(shuf -n 1 "$1")"
tr -dc 'A-Za-z0-9' dev/urandom | head -c 255 > "$fname"
printf "'%-3s'\n" "${str:$((ptr)):$((interval))}" >> "$fname"
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
I have to take the
$4parameter, divide it equally by$2parameter (If it cant be divided equally, add spaces to fill last part) and put every part at the end of randomized files we just created withIt looks like this:
script.sh process> .The number of files created depends on how many divided parts of the
$4by$2. Basically I need to equally hide part ofprocess> in randoms files which will be in. Also would it be easier to process files in the meantime so they can be sorted alphabetically in?Example :
script.sh 3 this will be divided .There "this will be divided" will be equally seperated in parts of 3 characters "thi" "s w" "ill" " be" " di" "vid" "ed ". In this example I needed to add a space at the end ofed. I will then send each individual parts at the end of my randomized files.