Shell Scripting

70 Tango Charlie

Well-Known Member
Hi Guys and Gals, {and all the ships at sea!} Walter Winchell, circa WW-2.
I got a little problem that I can't find any answer to.
I've been diddling with writing scripts a little bit so am a little familiar with the method of writing, saving, and making them executable.
The question I have is this - how do I write a script that will give me the lowest 5 of the last 8 numbers.
97, 84, 79, 88, 102, 89, 91, 78.
Let's say that I have them entered in a Calc spreadsheet in separate columns but the same row.
Thanks for your help in advance from the -
Old Geezer TC


Well-Known Member
In Linux, there is always a lot of ways to skin a cat.

I'm assuming your numbers are in a text file.

You could do something like this

sort -V -o sorted numbers

Where sorted is the name of a new text-file that gets created by the command above
and numbers is the text file that already has your randomly ordered numbers in it.

So you could add a second part.

tail -5 sorted

Which shows that last 5 numbers in the sorted file.

sort -V -o sorted-numbers random-numbers
tail -5 sorted-numbers

Maybe not the easiest way but it'll get you there.


Super Moderator
Staff member
Gold Supporter
G'day Charlie, nice choice ... scripting. :)

I am moving this Thread to Command Line, because although it is not clearly posted there, it is where scripting inquiries are best handled, and your Thread won't disappear off into the boonies as quickly as in General.

So you and @dos2unix hang on to your hats, buckle your seatbelts and take a magic carpet ride to Command Line.

Friday in Oz, so



Well-Known Member
@dos2unix - One minor adjustment - @70 Tango Charlie wants the lowest 5 numbers.
The sort command posted will sort them from lowest to highest - so he will need to use head -n 5, NOT tail -n 5.

Tail would be appropriate if sort's -r option was used, which reverses the sort, sorting from highest to lowest. Then the last 5 numbers in the sequence would be appropriate.
So the options would be:
sort -V numbers.txt | head -n 5 > lowestFive.txt
sort -Vr numbers.txt | tail -n 5 > lowestFive.txt
But the above would assume that each number is on a separate line in the file.

If Charlie exports the numbers to a comma separated file with the numbers in a single line like this:
97, 84, 79, 88, 102, 89, 91, 78

Then it's a little more complicated. But not much.
What you'd need to do in that case is parse out the delimiters using something like sed or awk and then sort the numbers and isolate the lowest five..
Using sed:
sed 's/,/\n/g;s/ //g;' numbers.txt | sort -V | head -n 5
Above replaces commas with newlines and removes any spaces.
So effectively, each number is on its own line. Then we can run the sort command and use head to display the lowest 5.

NOTE: There are a number of other ways that this could be done. This is just one of them!

The only problem with the above solution is the numbers will all be on different lines.
In a script, you'd probably want something that reports the result in a single line.

So to put the above solution into a one-liner you could use in a script - you could do something like this:
echo "The lowest five numbers in numbers.txt are: $(echo $(sed -e 's/,/\n/g;s/ //g;' numbers.txt | sort -V | head -n 5) | sed -e 's/\n/ /g')"
It's a little more complicated. We have some nested sub-shell's being invoked.
But it does it all in a single line.

The innermost sub-shell:
$(sed -e 's/,/\n/g;s/ //g;' numbers.txt | sort -V | head -n 5)
We've seen this already - this gets evaluated first - this yields our lowest 5 numbers.
But the numbers will be on separate lines.

Then the sub-shell surrounding it is evaluated:
$(echo $(lowest-5-result) | sed -e 's/\n/ /g')
NOTE:- Where lowest-5-result is actually the result of running the innermost sub-shell!
There is no variable called lowest-5-result, I've just substituted the entire expression in the innermost sub-shell with that, to represent its output when it is executed/evaluated!

That surrounding sub-shell strips out the newlines from our attempt to find the lowest 5 (innermost sub-shell), and replaces the newlines with spaces. So our lowest 5 numbers are now on the same line again.

Finally the echo command which encapsulates the two subshells are ran in the current shell. Displaying the message and the lowest 5 numbers:
The lowest numbers in numbers.txt are: 78 79 84 88 89
Last edited:

70 Tango Charlie

Well-Known Member
@dos2unix @JasKinasis
Thanks guys,
You have given me another lesson which I will have to study and see if I can absorb it into my little pea brain. LOL.
Until I have another question then........
AuRevoir or something like see you later.
Old Geez TC


Super Moderator
Staff member
Gold Supporter
Jas, question from a dummy here, if it does not derail the Thread?

Given Charlie has said

...I have them entered in a Calc spreadsheet...
and where you say

If Charlie exports the numbers to a comma separated file with the numbers in a single line like this:
Can that be used as CSV (comma separated values), which can then be read in a spreadsheet?



Staff online

Members online

Latest posts