[SOLVED] Data distribution among lines within a file with bash
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Data distribution among lines within a file with bash
Hi All
I need to create a text file and distribute some numbers among the lines by percentage. What do i mean exactly:
i want to set percentage for each number and then fill the lines by that percentage
500 - 20%
501 - 30%
502 - 50%
i need that 20% of the lines will contain the number "500", 30% for "501" and remaining 50% with "502". I need that to be filled random and not followed one by another.
Well, you can fill the file with numbers in sequence (according to their percentage) and scramble it later. You can try the shuf command or the shuffle function in perl, e.g.
Well, you can fill the file with numbers in sequence (according to their percentage) and scramble it later. You can try the shuf command or the shuffle function in perl, e.g.
Looking at the relevant piece of your script should be useful. At first I would generate the shuffled sequence of numbers then insert them in the original text one at a time in a loop.
for i in `seq -w 0 255`; do
for j in `seq -w 0 255`; do
echo -e "930000${i}${j},<here should come the distributed number>,,0,,930000${i}${j},930000${i}${j},English" >> test.txt
done
done
Ok. I would assign the shuffled sequence of numbers to an array and then use the Nth element of the array inside the loops, by increasing the index of the array by one at each iteration. Here we go:
The part in blue increases the variable c by one at each iteration. Using this specific notation (inherited from the C language) the variable is increased after it is evaluated. This means that at the first iteration the value is still 0, at the second iteration it is 1 and so on. This is exactly what we want, since array elements in bash are numbered starting from 0.
This looks very promising ! but i can't run it since my linux doesn't have the shuf. and i can't seem to find the rpm anywhere (RHEL 5.5 Tikanga 32bit). the official ISO doesn't have the rpm of it. Apparently coreutils rpm doesn't include it in RHEL 5.5...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.