![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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??)
|
|
|||
|
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}'
|
|
|||
|
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 |