How to find numbers in a textfile that are not divisible by 4096, round them up and write new file?
6
votes
7
answers
1253
views
In Linux there is a file
numbers.txt
.
It contains a few numbers, all separated with space.
There are numbers like this: 5476089856 71788143 9999744134 114731731 3179237376
In this example only the first and the last numbers are divisible by 4096. All others are not divisible by 4096. I need all numbers divisible by 4096.
The numbers which are not divisble by 4096 should be rounded up. The other ones should be untouched.
All of them I need written to a new file numbers_4096.txt
.
Numbers like the second 71788143
must be rounded up to 71790592
.
9999744134
to 9999745024
and so on...
Numbers like 5476089856
and 3179237376
should not be changed, they are divisible by 4096.
This script can do this with a single number:
#!/bin/sh
number=9999744134
divisor=4096
remainder=$((number % divisor))
new_number=$((number - remainder))
roundedup=$((new_number + divisor))
echo "Original number: $number"
echo "Divisor: $divisor"
echo "New number (divisible by $divisor): $new_number"
echo "Roundedup Number (divisible by $divisor): $roundedup"
But how to do that with a whole list that has to be rewritten and rounded up into a new file?
This script adds 4096 to numbers which are divisible by 4096, that is not what I want.
Asked by Banana
(189 rep)
Jun 17, 2025, 12:14 AM
Last activity: Jun 24, 2025, 09:37 AM
Last activity: Jun 24, 2025, 09:37 AM