so I have to generate 100 files in which would be any random number. Then I have to read those files and find in which file is the biggest number also sort all values and put everything in new .txt file. What I have to do first?

Thank you!

  • 5
    Sounds like a homework assignment. We can, perhaps, give hints (not sure), but you need to ask specific questions showing what you have already tried and why it isn't working. – Doug Smythies Dec 3 '19 at 21:31

First, generate all random numbers using $RANDOM in a loop and redirect them to the individual files, like that:

for i in {1..100}; do echo $RANDOM > $RANDOM.txt; done;

(There's a slight chance of duplicate numbers, so be aware of that.)

Next, read and sort these files, and tail just that last one. The full script will be something like that:


for i in {1..100}; do
    echo $RANDOM > ${i}.sample;

cat *.sample | sort | tail -n1;
| improve this answer | |
  • for i in {1..100} is a Bashism. Better (although still not fully portable): for i in $(seq 1 100). – fkraiem Dec 3 '19 at 21:57
  • @fkraiem $RANDOM is also a Bashism. Given that $RANDOM is being used, there's no good reason to avoid brace expansion. Anyway, this code has #!/bin/bash at the top. (It was always intended to be there, even before my edit. But it was placed on the same line as, and immediately after, the triple backticks that introduce a code fence. So it didn't appear, since that's where the optional name of a language given for purposes of specifying syntax highlighting goes.) – Eliah Kagan Dec 3 '19 at 23:00
  • 1
    I think you need to tell sort it is sorting numbers and not text. Also sorted output isn't going to a text file as the homework assignment requests. – WinEunuuchs2Unix Dec 4 '19 at 0:34
  • Guys, listen, it wasn't my intention to give the whole asnwer, just directions, ok? And about the bashisms, I'm aware of that. =) – Stefano Martins Dec 4 '19 at 13:16

Copyright James Daniel Marrs Ritchey. This material was created for submission at 'How to create 100 files', but can also be alternatively obtained from 'https://snippetly.blogspot.com/2019/12/create-100-files-with-random-numbers-in.html' under the terms of any of the following licenses: Comprehensible Open License 3.0 (https://jamesdanielmarrsritchey.blogspot.com/2019/06/comprehensible-open-license-30.html), MIT (https://opensource.org/licenses/MIT).

You haven't specified a scripting language so I'm assuming you are open to any language that can do what you need. You could use PHP. Just create a loop that generates a number and saves it to a file until it's created 100. Then read those 100 files into an array. Sort the array to have the numbers in order. Convert the array to a string, and save it to a new text file. Also get the last value from the array as this will be the largest number.

I'm not sure why you need to create all these files though. You could skip all that, and just create the final file containing all the numbers.


#Determine location of this script (so that files can be saved there)
$location = __DIR__;
#Create 100 files with random numbers in them. Files are saved to same directory as script. Numbers are between 0 and 1000000000.
for ($n = 1; $n <= 100; $n++) {
    file_put_contents("{$location}/{$n}.txt", random_int(0, 1000000000));
#Read numbers from the 100 files into a sorted new text file
$numbers = array();
for ($n = 1; $n <= 100; $n++) {
    $numbers[] = file_get_contents("{$location}/{$n}.txt");
sort($numbers, SORT_NUMERIC);
file_put_contents("{$location}/sorted_numbers.txt", implode("\n", $numbers));
#Determine the biggest number
$biggest_number = end($numbers);
echo "This biggest number is $biggest_number.\n";
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.