gsiva
January 19, 2018, 4:30am
1
hi ,
Let me put it in a different way with words.
Assume the lottery have numbers from 1-50.
Out of this 50 numbers, I am going to pick up only 35 numbers randomly.
so, my total numbers would be 35 numbers shuffled from [1-50] nos.
[eg: 1,4,6,7,11,30,32,40,46,49]
I have list of winning numbers in [my_number.txt] file.
Now, the script should check within my 35 numbers comparing the file [my_number.txt] and should give the results .. The result number should be 6 nos.
[eg: 1 6 30 32 46 49 ]
Thanks in Advance.
-Siva
rdrtx1
January 19, 2018, 10:52am
2
lottery_numbers=$(seq 1 50 | shuf -n 35 | sort -n | tr '\n' ' ' ; echo ;)
for i in `seq 10`; do seq 1 50 | shuf -n 6 | sort -g | tr '\n' ' ' ;echo; done > my_numbers
# or use input file
awk '
NR==1 {
print "";
print "Lottery Numbers: " lottery_numbers;
print "";
printf("%-25s\t%-10s\t%-10s\n", "My Numbers", "#s Matched", "Result")
n=split(lottery_numbers, ln);
}
{
c=0;
for (i=1; i<=NF; i++) for (j=1; j<=n ; j++) if ($i==ln[j]) c++;
printf("%-25s\t%-10d\t%-10s\n", $0, c, ((c < 6) ? "lose" : "win"));
}
' lottery_numbers="$lottery_numbers" my_numbers
rbatte1
January 19, 2018, 10:58am
3
Is this not the same as https://www.unix.com/shell-programming-and-scripting/276392-lottery-result-checker.html ?
Are you actually the same person and the login names have become muddled?
Robin
gsiva
January 20, 2018, 9:37am
4
Hi rdrtx1 ,
Thanks for your input..
Between, is there any way that I can manually input the 35 numbers rather than auto shuffle from 1-50 number to pick up only 35 nos..
-Siva
rdrtx1
January 21, 2018, 10:35pm
5
mx=50
mn=35
arr=""
c=$(echo "$arr" | wc -w)
while [ $c -lt $mn ]
do
(( t = mn - c ))
echo "Enter numbers from 1 to 50 ($t to go):"
read numbers
numbers=$(echo "$numbers" | sed 's/[;,:\//]/ /g;' | awk '
{
c=split(arr, a);
for (i=1; i<=c; i++) b[a]=i;
for (i=1; i<=NF; i++) {
n=$i;
n+=0;
n=int(n);
if (n > 0 && n<=mx && ! b[n] && split(arr, _) < mn) {arr=arr " " n; b[n]=n;}
}
if (arr) print arr;
} ' mn=$mn mx=$mx arr="$arr";
)
arr=$(echo "$numbers" | sed 's/^ *//; s/ *$//; s/ */ /g;')
c=$(echo "$arr" | wc -w)
echo ""
echo "Numbers entered: $arr"
echo ""
done
gsiva
January 29, 2018, 4:12am
6
Hi rdrtx,
I am lost here.. Do I need to use only the second script or I need to merge the first and second script together ?
-Siva
gsiva
February 1, 2018, 4:55am
7
Hi ,
The second scripts only ask for the 35 numbers, so once i enter the 35 nos, its not getting me any output...
-Siva
If your shell supports arrays this change based on rdrtx1's original may suffice:
mx=50
mn=35
while (( ${#LN[@]} < mn ))
do
read -p "Enter numbers from 1 to 50 ($((mn - ${#LN[@]})) to go): " numbers
for num in $numbers
do
((num < 1 || num > mx)) && echo "$num is invalid" || LN[num]=$num
(( ${#LN[@]} == mn )) && break 2
done
echo "Numbers entered so far: ${LN[@]}"
done
for i in {1..10}; do printf "%d\n" {1..50} | shuf -n 6 | sort -g | tr '\n' ' ' ;echo; done > my_numbers
# or use input file
awk '
NR==1 {
print "";
print "Lottery Numbers: " lottery_numbers;
print "";
printf("%-25s\t%-10s\t%-10s\n", "My Numbers", "#s Matched", "Result")
n=split(lottery_numbers, ln);
}
{
c=0;
for (i=1; i<=NF; i++) for (j=1; j<=n ; j++) if ($i==ln[j]) c++;
printf("%-25s\t%-10d\t%-10s\n", $0, c, ((c < 6) ? "lose" : "win"));
}
' lottery_numbers="${LN[*]}" my_numbers
gsiva
February 1, 2018, 11:03pm
9
Hi Chubler,
The script worked out well with your modification.. Thanks a lot...
Between, I got what I expected.
[root@linux lottery]# sh script.sh
Enter numbers from 1 to 50 (35 to go): 2 19 47 1 46 3 6 11 15 17 26 29 32 34 40 44 48 49 5 7 9 13 22 23 25 27 45 4 8 10 14 16 18 30 35
Lottery Numbers: 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 22 23 25 26 27 29 30 32 34 35 40 44 45 46 47 48 49
My Numbers #s Matched Result
4 17 21 25 33 48 4 lose
10 24 26 28 32 44 4 lose
5 7 24 44 48 49 5 lose
3 7 17 23 32 47 6 win
5 28 34 36 38 45 3 lose
18 20 27 43 48 49 4 lose
But with this result, i need to generate the sequence of 6 nos within the 35 numbers i have mentioned and need to compare with the my_numbers file.
Is this the sort of thing you mean?
mx=50
mn=35
while (( ${#LN[@]} < mn ))
do
read -p "Enter numbers from 1 to 50 ($((mn - ${#LN[@]})) to go): " numbers
for num in $numbers
do
[[ $num =~ ^([[:digit:]])+$ ]] && ((num > 0 && num <= mx)) && LN[num]=$num ||
echo "$num is invalid"
(( ${#LN[@]} == mn )) && break 2
done
echo "Numbers entered so far: ${LN[@]}"
done
for i in {1..10}; do printf "%d\n" {1..50} | shuf -n 6 | sort -g | tr '\n' ' ' ;echo; done > my_numbers
lottery_numbers=$(printf "%d\n" ${LN[@]}| shuf -n 6 | sort -g| tr '\n' ' ')
# or use input file
awk '
NR==1 {
print "";
print "Lottery Numbers: " lottery_numbers;
print "";
printf("%-25s\t%-10s\t%-10s\n", "My Numbers", "#s Matched", "Result")
n=split(lottery_numbers, ln);
}
{
c=0;
for (i=1; i<=NF; i++) for (j=1; j<=n ; j++) if ($i==ln[j]) c++;
printf("%-25s\t%-10d\t%-10s\n", $0, c, ((c < 6) ? "lose" : "win"));
}
' lottery_numbers="$lottery_numbers" my_numbers