help needed to put instance numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help needed to put instance numbers
# 8  
Old 07-16-2012
hi Pikk45

it is not working ...

total number of records is 7 am splitting into 1 0r 2 0r 3 as passing parameter value

if it is 1 means
1,emp
1,dept
1,class
1,subclass
1,region
1,country
1,division
if 2 means

1,emp
1,dept
1,class
1,subclass
2,region
2,country
2,division

if 3 means

1,emp
1,dept
1,class
2,subclass
2,region
2,country
3,division
# 9  
Old 07-16-2012
Code:
#! /usr/bin/perl -w
use strict;

open I, "< inputfile.txt";
for (<I>) {};
my $cnt = $.;
close I;

my $intDiv = int ( $cnt / $ARGV[0] );
my $div    = $cnt / $ARGV[0];
my $x      = $div - $intDiv == 0 ? $intDiv : $intDiv + 1;
my $i      = 1;

open I, "< inputfile.txt";
while (<I>) {
    chomp;
    print "$i,$_\n";
    if ( $. % $x == 0 ) { $i++ };
}
close I;

Few runs:
Code:
[user@host ~]# ./test.pl 3
1,emp
1,dept
1,class
2,subclass
2,region
2,country
3,division
[user@host ~]# ./test.pl 2
1,emp
1,dept
1,class
1,subclass
2,region
2,country
2,division
[user@host ~]# ./test.pl 1
1,emp
1,dept
1,class
1,subclass
1,region
1,country
1,division
[user@host ~]#


Last edited by balajesuri; 07-16-2012 at 07:15 AM..
This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hint needed for incrementing numbers

Hi All Been trying to get something working but having some trouble in unix bash or ksh scripting. Im trying to increment once a condition has been met Say I have a file that contains: apple orange banana grapes dates kiwi What im after is once a counter has reached every second... (3 Replies)
Discussion started by: chandika_diran
3 Replies

2. Shell Programming and Scripting

Put numbers after word

Hello I have an file with this content -------------------------------------------- timer one timer two timer three timer four timer five timer six timer seven ------------------------------------------- And I want the following output.... (4 Replies)
Discussion started by: thailand
4 Replies

3. Shell Programming and Scripting

Help extracting single instance of numbers which repeat

Hi, the title isn't very descriptive but it'll be easier to explain what I need if I write out the coordinates from which I need to extract certain information: ATOM 2521 C MAM X 61 44.622 49.357 12.584 1.00 0.00 C ATOM 2522 H MAM X 61 43.644 49.102 12.205 ... (10 Replies)
Discussion started by: crunchgargoyle
10 Replies

4. Shell Programming and Scripting

Put double quotes around numbers

Hi, consider a file which has data such as "random text",912345,"54","finish" "random text",9991236745,"9954","finish" I want to replace the numbers that don't have double quotes around them with ones that do; so the output should be "random text","912345","54","finish" "random... (4 Replies)
Discussion started by: Storms
4 Replies

5. AIX

HACMP 5.4.1->5.5 offline upgrade - different instance numbers ?

Hello, I did offline HACMP(PowerHA) upgrade 5.4.1 to 5.5 - basically stopped HACMP services and upgraded cluster.* filesets. Tried to start services again - topsvcs refused to start on second node complaining that node instance numbers are different - and indeed they are different. This is... (2 Replies)
Discussion started by: vilius
2 Replies

6. Shell Programming and Scripting

Put numbers against the words

Hi All, I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files. I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using sort -u from the actual .dat files.... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. Shell Programming and Scripting

sed appending needed only after first instance

Hi, Here is my piece of code used with sed in shell script: sed -i '/<falsemodule-option>/ a\<LdapLogin>' myxmlfile The problem that i am facing with the above is that in 'myxml' file i have mulitple instances of <falsemodule-option> so when i execute the above sed command, it is appending... (10 Replies)
Discussion started by: sunrexstar
10 Replies

8. Solaris

help needed to put machine in domain

Hi All, I am working on Solaris sparc 9 and my computer name is mgsun. I want to bring this machine to a specific domain (e.g.-xx.xx.org). So please help me to do the same.. thanks in Advance... (4 Replies)
Discussion started by: smartgupta
4 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies
Login or Register to Ask a Question