The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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-27-2007 02:02 AM
checking jump sequence number (part2) happyv Shell Programming and Scripting 3 10-19-2006 06:03 AM
SQL Loader Auto Number Generation vinoth_kumar UNIX for Dummies Questions & Answers 2 08-01-2006 08:40 AM
Random number generation tej.buch High Level Programming 1 02-13-2006 10:07 AM
sequence number checking nhatch UNIX for Dummies Questions & Answers 1 04-24-2003 02:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-26-2008
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
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
  #2 (permalink)  
Old 03-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 03-26-2008
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
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
  #4 (permalink)  
Old 03-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 03-26-2008
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
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
  #6 (permalink)  
Old 03-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 }'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0