The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Random number generation in ksh mervin2006 UNIX for Dummies Questions & Answers 2 04-26-2007 11:02 PM
checking jump sequence number (part2) happyv Shell Programming and Scripting 3 10-19-2006 03:03 AM
SQL Loader Auto Number Generation vinoth_kumar UNIX for Dummies Questions & Answers 2 08-01-2006 05:40 AM
Random number generation tej.buch High Level Programming 1 02-13-2006 06:07 AM
sequence number checking nhatch UNIX for Dummies Questions & Answers 1 04-24-2003 11:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Sep 2006
Posts: 75
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
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??)
Reply With Quote
  #3 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Sep 2006
Posts: 75
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
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}'
Reply With Quote
  #5 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Sep 2006
Posts: 75
Stumble this Post!
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
Reply With Quote
  #6 (permalink)  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
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 }'
Reply With Quote
  #7 (permalink)  
Old 03-27-2008
Registered User
 

Join Date: Sep 2006
Posts: 75
Stumble this Post!
I worked great

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

Thanks a lot
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0