Visit Our UNIX and Linux User Community


generate level numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting generate level numbers
# 8  
Old 09-22-2007
Put the script back the way I wrote it. You cannot introduce random changes to my script and expect it to continue to work. I will test my script, not yours. My script with E in the data:
$ cat datae
A 0
B 0
C 0
D 0
E A
E B
E C
Z H
Z D
F E
G E
H E
I G
$ ./level datae
A 0 1
B 0 1
C 0 1
D 0 1
E A 2
E B 2
E C 2
F E 3
G E 3
H E 3
I G 4
Z D 4
Z H 4
$

And my script with P in the data:
$ cat datap
A 0
B 0
C 0
D 0
P A
P B
P C
Z H
Z D
F P
G P
H P
I G
$ ./level datap
A 0 1
B 0 1
C 0 1
P A 2
P B 2
P C 2
D 0 1
F P 3
G P 3
H P 3
I G 4
Z D 4
Z H 4
$

The only difference is that the "D 0 1" moved below the E/P lines. That is an artifact of tsort, there are several possible ways to order the data and tsort displays one of them. If this bothers you, use a final sort to sort the output.
# 9  
Old 09-22-2007
I am very sorry Perderabo... i didnot mean that way...

I thought u made a typo error because

when i run with E i get


[tofps8]/home/tofps8/SCRIPTS> cat datae
A 0
B 0
C 0
D 0
E A
E B
E C
Z H
Z D
F E
G E
H E
I G

[tofps8]/home/tofps8/SCRIPTS> ./level datae
A 3 1
B 2 1
C 1 1
D 6 1
E 4 1
E 4 1
E 4 1
F 10 1
G 8 1
H 5 1
I 9 1
Z 7 1
Z 7 1

[tofps8]/home/tofps8/SCRIPTS> cat level
#! /usr/bin/ksh
sort $1 > temp1
tsort temp1 | sed '$d' | nl | sort -nr | cut -f2 | nl | awk '{print $2, $1}' | sort | join temp1 - | sort -k 3n | awk '{print $1,$2}' |\
awk '{r[NR]=$0 ; k[NR]=$1 ; if(v[$1]<1+v[$2]) v[$1]=1+v[$2] } END { for (n=1; n<=NR; n++) print r[n] , v[k[n]]}'
exit 0



and for data with P

[tofps8]/home/tofps8/SCRIPTS> ./level datap
A 3 1
B 2 1
C 1 1
D 6 1
F 10 1
G 8 1
H 5 1
I 9 1
P 4 1
P 4 1
P 4 1
Z 7 1
Z 7 1

i just copy pasted what u gave...

I use AIX 5.3... any issue in it..

Last edited by pbsrinivas; 09-22-2007 at 10:10 AM..
# 10  
Old 09-22-2007
Don't know what to tell you. I don't have AIX.
# 11  
Old 09-22-2007
I have troubled u a lot.... Smilie

let me explain what is happening on my machine..

till this both give same out put

Quote:
tsort temp1 | sed '$d' | nl | sort -nr | cut -f2 | nl | awk '{print $2, $1}'
after that
Quote:
sort | join temp1 -
changes the order..



but i dont know y it shows as all 1's in the 3rd column...

and then
Quote:
| sort -k 3n | awk '{print $1,$2}' |
that gets me the Job name and the interger ... doestnt get me the job and predecessor as expected...

thats y i thought that i might be
Quote:
awk '{print $1,$2}' |\
instead of
Quote:
awk '{print $1,$3}' |\
..

the join of temp1 and - doesnt get me the job and predeessor...

Please throw some light on it...

thanks for all the help...



if i am right

sort $1 > temp1
tsort temp1 --- does the topological sort
sed '$d' --- deletes the last line which is 0 in our case
nl --- appends line numbers to each line
sort -nr --- sort the file in reverse based on the line numbers given by previous nl command
cut -f2 --- gets the job name back ...
nl --- again a line number appending
awk '{print $2, $1}'--- gets the job name followed by line number to be printed
sort --- sort on the job name
join temp1 - --- ??????? dont know exactly what it does.... { i thought it joins the Job name to its predecessor}

sort -k 3n ---- sort on the 3rd field
awk '{print $1,$2}' -- prints the first two columns... (in this case i thought the first 2 colums would be the job name, line number )

the next login i flaw less i think the tsort is fine....

awk '{r[NR]=$0 ; k[NR]=$1 ; if(v[$1]<1+v[$2]) v[$1]=1+v[$2] } END { for (n=1; n<=NR; n++) print r[n] , v[k[n]]}'



I am counting on You people only....
Please dont desert me..... Smilie

Last edited by pbsrinivas; 09-22-2007 at 04:23 PM..
# 12  
Old 09-22-2007
join does a "join" operation on two files, where "join" is the relational database join. Since I did not specify otherwise, the first field of both files is the key field and the files must be sorted on the field. Example:

Code:
$ cat one
dog puppy
cat kitty
$ cat two
dog fido
dog rover
dog spot
cat fluffy
cat tiger
cat felix
$ join one two
dog puppy fido
dog puppy rover
dog puppy spot
cat kitty fluffy
cat kitty tiger
cat kitty felix

The first output field is the key field for both files. Then comes the data fields for the first file and then come the data fields for the second file. This is how join is supposed to work. You seem to be saying that your join is giving the key field, then data field for the 2nd file, and then the data field for the first file. It's not supposed to do that. But I can't test on AIX. If it is backwards, then your awk change should compensate. But if you still get wrong output, something else must not be right. Not sure what... Smilie
# 13  
Old 09-24-2007
Thanks Perderbo..

I made it to work on AIX

Quote:
tsort temp1 | sed '$d' | nl | sort -nr | cut -f2 |
nl | awk '{print $2, $1}' | sort | join temp1 - |sort -k 2n | awk '{print $1,$3}'| awk '{r[NR]=$0 ; k[NR]=$1 ; if(v[$1]<1+v[$2]) v[$1]=1+v[$2] } END { for (n=1; n<=NR; n++) print r[n] , v[k[n]]}'|grep "26"
I have sorted on 2 nd feild rather than on 3rd and printed the 1st and 3rd columns...

Last time the sort was on 3rd feild so it did not work

Now it works great...

Thanks a lot for all the support...

Previous Thread | Next Thread
Test Your Knowledge in Computers #962
Difficulty: Medium
ILOVEYOU, sometimes referred to as Love Bug or Love Letter, was a computer worm that attacked tens of millions of Windows PCs on and after 5 May 2000 local time in the Philippines.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate sequence of numbers

I need awk script to generate part number sequencing based on data in multiple columns like below Input File --------- Col A|Col B|Col C| 1|a|x| 2|b|y| |c|z| | |m| | |n| And out put should be like 1ax 1ay 1az 1am 1an 1bx 1by (6 Replies)
Discussion started by: aramacha
6 Replies

2. Shell Programming and Scripting

Need advise to generate 10 character numbers example 0000000000 - 9999999999

Hi all, i need advise from all experts here. when i use command below to print number in 10 character printf "%010d\n" {0..999} it will give me output nicely starts from 0000000000 untillllllllllll 0000000994 0000000995 0000000996 0000000997 0000000998 0000000999 However. when i... (6 Replies)
Discussion started by: jason6247
6 Replies

3. Shell Programming and Scripting

Auto generate Line Numbers

How do I generate line numbers in Vi? I have this: ,'04-90020-039N','61423','2GDV00039-0002', SYSDATE); ,'04-90020-040D','61423','2GDV00046-0001', SYSDATE); ,'04-90020-041N','61423','2GDV00038-0002', SYSDATE); ,'04-90020-043D','61423','2GDV00047-0001', SYSDATE);... (3 Replies)
Discussion started by: djehresmann
3 Replies

4. Shell Programming and Scripting

Generate random numbers in script

i want to generate a random number through a script, and even if anyone reads the script, they wont be able to figure out what the random number is. only the person who setup the script would know it. something like this could work: random the full thread is here: ... (13 Replies)
Discussion started by: SkySmart
13 Replies

5. Shell Programming and Scripting

Generate 16 digit positive random Numbers

Hi Unix Gurus, I have a requirement to generate positive random 16 and 13 digit numbers. Here is the script I have so far..... number=$RANDOM$RANDOM$RANDOM$RANDOM; let "number %= 10000000000000"; echo $number But sometimes it is generating negative numbers and also 15 digit... (8 Replies)
Discussion started by: scorpioraghu
8 Replies

6. Shell Programming and Scripting

Generate Codes based on start and End values of numbers in a column

Hello All, Could you please help with this. This is what I have: 506234.222 2 506234.222 2 506234.222 2 506234.222 2 508212.200 2 508212.200 2 333456.111 2 333456.111 2 333456.111 2 333456.111 2 But this is what I want: 506234.222 1 506234.222 2 506234.222 2 506234.222 3 (5 Replies)
Discussion started by: canimba
5 Replies

7. Programming

generate array of random numbers

hi guys, I am writing a c program that generates a two dimensional array to make matrix and a vector of random numbers and perform multiplication. I can't figure out whats wrong with my code. It generates a matrix of random numbers but all the numbers in the vector array is same and so is the... (2 Replies)
Discussion started by: saboture88
2 Replies

8. Shell Programming and Scripting

Generate numbers 000 to 999

I have tried to make this script to generate: 000 001 002 ... 997 998 999 i=0 while do if then echo "00"$i else if && then echo "0"$i (5 Replies)
Discussion started by: locoroco
5 Replies

9. Shell Programming and Scripting

How to generate 10.000 unique numbers?

hello, does anybody can give me a hint on how to generate a lot of numbers which are not identically via scripting etc? (7 Replies)
Discussion started by: xrays
7 Replies

10. Shell Programming and Scripting

How to generate a series of numbers

Hi All, I have a requirement where in I have an input as follows:- input=1-4,6,8-10,12-15 I need to explode this range into an output file as follows:- 1 2 3 4 6 8 9 10 12 13 14 15 My input may vary like 1,5-9,11-13,15-17....... (3 Replies)
Discussion started by: rony_daniel
3 Replies

Featured Tech Videos