assigning counter to same keys in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assigning counter to same keys in a file
# 1  
Old 10-01-2008
assigning counter to same keys in a file

Hi,
I've a data file with similar keys coming in. I want to assign an incremental counter to those records and attach to a file

for example

File
10001 ABCD
10002 PQRS
10001 ABCD
10003 QWER
10001 ABCD
10002 PQRS
10004 POIU

output as
10001 ABCD 1
10002 PQRS 1
10001 ABCD 2
10003 QWER 1
10001 ABCD 3
10002 PQRS 2
10004 POIU 1

any help guys....
can we do it with awk?

Thanks
# 2  
Old 10-01-2008
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk '$(NF+1)=++c[$0]' filename

If you want to preserve consecutive default FS characters:

Code:
awk '{print$0,++c[$0]}' filename


Last edited by radoulov; 10-01-2008 at 05:05 PM.. Reason: changed $2 to $0
# 3  
Old 10-01-2008
Code:
awk '{a[$1]++}$0=$0 FS a[$1]' file

# 4  
Old 10-01-2008
Thanks, exactly what i was looking for...
could you please explain?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace a string in a file with a counter.

I have a file like this : location 100 SlotNumber ..some lines inbetween location 150 SlotNumber ..some lines inbetween location 160 SlotNumber ..some lines inbetween I want to replace the SlotNumber like SlotNumber: location 100 SlotNumber1 ..some lines inbetween location 150... (1 Reply)
Discussion started by: naga_raj
1 Replies

2. Shell Programming and Scripting

Help on Adding one counter loop at the end of each line in a file

Hello All, I have file a.txt I want to add a counter loop at the end of each line in a file ill explain: i have a site h**p://test.test=Elite#1 i want to add a a counter to the number at the end of the file, that it will be like this urlLink//test.test=Elite#1 urlLink//test.test=Elite#2... (3 Replies)
Discussion started by: nexsus
3 Replies

3. Homework & Coursework Questions

Min/Max/counter/while loop from file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The program is supposed to read in text from a given file ( different samples provided in the homework but not... (1 Reply)
Discussion started by: c++newb
1 Replies

4. Shell Programming and Scripting

File merging based on different counter loop

hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ... (0 Replies)
Discussion started by: manas_ranjan
0 Replies

5. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

6. Shell Programming and Scripting

parsing data from a big file using keys from another smaller file

Hi, I have 2 files format of file 1 is: a1 b2 a2 c2 d1 f3 format of file 2 is (tab delimited): a1 1.2 0.5 0.06 0.7 0.9 1 0.023 a3 0.91 0.007 0.12 0.34 0.45 1 0.7 a2 1.05 2.3 0.25 1 0.9 0.3 0.091 b1 1 5.4 0.3 9.2 0.3 0.2 0.1 b2 3 5 7 0.9 1 9 0 1 b3 0.001 1 2.3 4.6 8.9 10 0 1 0... (10 Replies)
Discussion started by: Lucky Ali
10 Replies

7. Shell Programming and Scripting

dsa keys - different pub file

Hi I wish to set up sftp between server1 and server2. File needs to be moved from server1 to server2. I have logged into server1 with my credentials and cd to .ssh using ssh-keygen -t dsa , i have generated the keys and stores in id_dsa.pub and id_dsa file. Later i have moved the contents of... (3 Replies)
Discussion started by: forums123456
3 Replies

8. Shell Programming and Scripting

Problem assigning a counter for particular pattern

Hi, I have a script that compares two files(which are updated dynamically by a daemon) and evaluate results from the comparision. For the first line of comparision from the file1, i will grep some part of the line in file with file1 and set a counter for that particular comparison. So for each... (12 Replies)
Discussion started by: reddybs
12 Replies

9. Programming

WRT counter show me that line from a txt file

Hello Experts, I am beginner to C language.. I know I am making a simple mistake but I dont know what the problem here #include <stdlib.h> #include <stdio.h> int main(void) { FILE* fh; char s; int i; int count =10; double a,b; printf("COUNT=... (6 Replies)
Discussion started by: user_prady
6 Replies

10. UNIX for Dummies Questions & Answers

arrow keys / special keys

how to use the arrow keys in shell scripting. is there any special synatax / command for this. i just want to use the arrow keys for navigation. replies appreciated raguram R (3 Replies)
Discussion started by: raguramtgr
3 Replies
Login or Register to Ask a Question