Sequence number generation on a key column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sequence number generation on a key column
# 1  
Old 03-26-2008
Sequence number generation on a key column

Hi Folks,

Can you help me with this issue:

I have to generate the numbers say from 1001 for each record in a file based on a key field,
the catch is the generated number should be unique based on key column.
(EMP_NUMBER)

Example:

Input File:
EMP_NUMBER EMP_NAME

8908 SMITH
8100 DAVID
8100 DAVID
8230 MARK
8240 GEORGE
8100 DAVID
8100 DAVID

Output File:

8908 SMITH 1001
8100 DAVID 1002
8100 DAVID 1002
8230 MARK 1003
8240 GEORGE 1004
8100 DAVID 1005
8100 DAVID 1005



Thanks in advance.

S Smilie
# 2  
Old 03-26-2008
Hhuh? So adjacent DAVIDs should get the same number but different from another identical bunch of DAVIDs further down in the file? (And what cruel parents gave their children names in all uppercase??)
# 3  
Old 03-26-2008
Actually we have to compare the current record (EMPLOYEE_NUMBER) with the previous records (EMPLOYEE_NUMBER),
If different the seq. number has to be generated otherwise
the number remains same for that record,


Thank you
S Smilie
# 4  
Old 03-26-2008
I'm not sure if that was supposed to mean "no, sorry, the example was wrong; they're all the same DAVID" or "yes, sorry, the stuff about unique per key wasn't right actually" but assuming the former, how about

Code:
awk 'BEGIN { n = 1000 } { n++ if (k["$0"]++ == 0); print $0, $n}'

# 5  
Old 03-26-2008
It is actually later:
"yes, sorry, the stuff about unique per key wasn't right actually"

Please find the request:

I have to generate the numbers say from 1001 for each record in a file based on a key field (EMPLOYEE_NUMBER),

we have to compare the current record (EMPLOYEE_NUMBER) with the previous records (EMPLOYEE_NUMBER),
If different the seq. number has to be generated otherwise
the number remains same for that record,


Input File:
EMP_NUMBER EMP_NAME

8908 SMITH
8100 DAVID
8100 DAVID
8230 MARK
8240 GEORGE
8100 DAVID
8100 DAVID

Output File:
Gen_Key
8908 SMITH 1001
8100 DAVID 1002
8100 DAVID 1002
8230 MARK 1003
8240 GEORGE 1004
8100 DAVID 1005
8100 DAVID 1005





Thanks

S Smilie
# 6  
Old 03-27-2008
And you are not able to modify the simple awk script you were already given?

Code:
awk 'BEGIN { n = 1000 } { if ($0 != prev) { n++ } print $0, n; prev = $0 }'

# 7  
Old 03-27-2008
I worked great

awk 'BEGIN { n = 100} { if ($1!= prev) { n++ } prev = $1; print $1,$2,n }'

Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

2. Shell Programming and Scripting

Sequence generation in shell

Hi, I am using the nested for loops to generate the sequence , taking start and end sequence number input from test.txt (sample content mentioned below). Also , can I print the rest of columns as well with sequence number into the same file. for i in `cat test.txt|cut -d"," -f7` do ... (8 Replies)
Discussion started by: tushar.modgil
8 Replies

3. UNIX for Advanced & Expert Users

scp without first time key generation and confirmation

Hi All, I have setup ssh keys for a user to transfer files without password between two hosts. But still the first time scp asks for confirmation and generates a key Due to this our jobs are failing. Is there a way I can avoid that first time key generation and confirmation also and automate... (3 Replies)
Discussion started by: baanprog
3 Replies

4. UNIX for Dummies Questions & Answers

Public and Private Key generation for scp

Hi, What tool is used to generate public and private keys for SCP? Do you have an example script that generates these keys, puts them in files and then another example script that references them from SCP? Thanks, (9 Replies)
Discussion started by: Astrocloud
9 Replies

5. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

7. Shell Programming and Scripting

Press Any Key script sequence using bash - HELP

hi to all. im a newbie in unix shell scripts. i want to make a simple unix shell script using the bash shell that asks a user to press any key after a series of commands, or an x if he wishes to exit. here's a sample script that i made: #!/usr/bin/bash pause(){ /usr/bin/echo "\t\t Press... (3 Replies)
Discussion started by: booghaw
3 Replies

8. Shell Programming and Scripting

ID generation in sequence

i have an xml tag. The value for the tag should be iterated through out the xml document. eg: <data><id><id><name>a</name><addr>aaa</addr><phnumb>3456</phnumb><state>ca</state><city>ny</city></data>... (6 Replies)
Discussion started by: Sgiri1
6 Replies

9. Shell Programming and Scripting

automating RSA key pair generation

I want to automate the process of generating RSA keys. I want to remotley login to a linux machine from a windows maching without having to enter a password. For this I need to generate the RSA key pair. but I want to do this procedure on alot of linux machines. For which I was looking to automate... (2 Replies)
Discussion started by: lassimanji
2 Replies

10. UNIX and Linux Applications

SSH: Avoiding password prompt with rsa key generation

Hi, I am using a remote storage service for backing up our data - we want to have a script run as part of a cron job which would do the backups from our local Linux machine to the service's Linux machine. I want to use tar and ssh to do this (rather than mounting the disk and using cp) .... (5 Replies)
Discussion started by: same1290
5 Replies
Login or Register to Ask a Question