Reputation: 207
For example if the number is 4569
.
i need the output as
1 - 1000
1001 - 2000
2001 - 3000
3001 - 4000
4000 - 4569
I wrote the logic in JAVA for this now but I am migrating my code to UNIX. Is there any single liner to split the numbers with specific range?
Upvotes: 0
Views: 717
Reputation: 47137
Or POSIX compatible:
#!/bin/sh
max=4569
num=1
while [ "$num" -lt "$max" ]; do
next=`expr "$num" + "999"`
[ "$next" -gt "$max" ] && next="$max"
echo "$num - $next"
num=`expr "$next" + "1"`
done
Upvotes: 1
Reputation: 2672
With awk
:
awk 'BEGIN{e=999;m=4569;for(i=1;i<=m;i+=1000){k=m-i;if(k<1000){e=k};print i" - "i+e}}'
Upvotes: 2
Reputation: 1054
There may be a one-liner with awk. If you have a recent bash you could use bash arithmetics. It's not as fast tho.
sample.sh
#!/bin/bash
(( max = $1 ))
(( num = 0 ))
echo "going for " $max
while (( max >= num )); do
(( prev = num + 1 ))
(( num = prev + 999 ))
if (( num >= max )); then
echo $prev "-" $max
else
echo $prev "-" $num
fi
done
which prints
$ ./sample.sh 4569
going for 4569
1 - 1000
1001 - 2000
2001 - 3000
3001 - 4000
4001 - 4569
You can modify it accordingly.
Upvotes: 1