Looping help to split the total


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping help to split the total
# 1  
Old 06-12-2013
Looping help to split the total

Hi Gurus,


Please help me in below requirement.

Instance =5 (it is user parameter)

total=52 (it is user parameter

i need to split this to 5 and reminder as 1 instances totally 6

for example i need to splitt to each

52/5=10.4

1-10
11-20
21-30
31-40
41-50
51-52

i need this in file

Thanks
in advance

---------- Post updated at 01:28 AM ---------- Previous update was at 12:52 AM ----------

Please help.....
# 2  
Old 06-12-2013
Code:
#!/bin/bash

instance=$1
total=$2

div=$(( total / instance ))
diff=$(( total % instance ))

for (( i=1; i <= $instance; i++ ))
do
    start=$(( ((i-1)*div) + 1 ))
    end=$(( div * i ))
    echo "$start - $end"
done

if [ $diff -ne 0 ]; then
  start=$(( ( i * div ) + 1 ))
  end=$(( (i*div)+diff ))
  echo "$start - $end"
fi


# END

$ ./kkk.sh 5 65
1 - 13
14 - 26
27 - 39
40 - 52
53 - 65

# 3  
Old 06-12-2013
Code:
echo "5 52" | awk '{s=int($2/$1);r=$2-s*$1;for (i=0;i<$1;i++) {for (j=0;j<s;j++) printf "%s ",i*10+j;print ""};for (i=0;i<r;i++) printf "%s ",i+s*$1;print ""}'
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51

Some more readable
Code:
echo "5 52" | awk '{
	s=int($2/$1)
	r=$2-s*$1
	for (i=0;i<$1;i++) 
		{
		for (j=0;j<s;j++)
			printf "%s ",i*10+j
			print ""
		}
	for (i=0;i<r;i++) 
		printf "%s ",i+s*$1
		print ""
	}'


Last edited by Jotne; 06-12-2013 at 04:52 AM..
This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

2. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

4. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

5. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

6. Shell Programming and Scripting

help on looping using if/for or while

Hello, where can I get usefull information on the use of looping with for , if and while with extensive examples. Also use of variables in scripts (1 Reply)
Discussion started by: sam4now
1 Replies

7. Shell Programming and Scripting

for looping

I run into a issue when I try to do sorting of the following with ascending order, one round of for looping seems not working, anyone knows how to use shell or perl? $array = (5,0,3,2,7,9,8) (2 Replies)
Discussion started by: ccp
2 Replies

8. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

9. UNIX for Dummies Questions & Answers

grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to... (5 Replies)
Discussion started by: MrAd
5 Replies
Login or Register to Ask a Question