Add a Group ID to each row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a Group ID to each row
# 1  
Old 10-15-2012
Add a Group ID to each row

I have a sample data like this:
user1
1001
role1

user2
1002
role1
role2

user3
1003
role1
role2
role3
And I need to convert it like this:
1,user1
1,1001
1,role1

2,user2
2,1002
2,role1
2,role2

3,user3
3,1003
3,role1
3,role2
3,role3
Thank you for your help and inputs.

Kumar
# 2  
Old 10-15-2012
First , reformat your post -> CODE TAGS
An awk:
Code:
#awk '$0==""{c++;print;next }{print c","$0}' c=1  infile

# 3  
Old 10-16-2012
Thank you. I am also seeing if its possible to extend the output like this:
Code:
1,user1,1
1,1001,2
1,role1,3

2,user2,1
2,1002,2
2,role1,3
2,role2,4

3,user3,1
3,1003,2
3,role1,3
3,role2,4
3,role3,5

Thank you. Appreciate your help.
# 4  
Old 10-16-2012
Code:
awk '!NF{c++;cc=0;print;next }{print c,$0,++cc}' c=1 OFS=,  infile

# 5  
Old 10-16-2012
I am getting this error:

Code:
awk: syntax error near line 1
awk: bailing out near line 1

Can you pls help?
# 6  
Old 10-16-2012
Quote:
Originally Posted by vskr72
I am getting this error:

Code:
awk: syntax error near line 1
awk: bailing out near line 1

Can you pls help?
if on Solaris, use 'nawk' instead of 'awk'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter Row Based On Max Column Value After Group BY

Hello Team, Need your expertise on following: Here is the set of data: C1|4|C1SP1|A1|C1BP1|T1 C1|4|C1SP2|A1|C1BP2|T2 C2|3|C2SP1|A2|C2BP1|T2 C3|3|C3SP1|A3|C3BP1|T2 C2|2|C2SP2|A2|C2BP2|T1 I need to filter above date base on following two steps: 1. Group them by column 1 and 4 2.... (12 Replies)
Discussion started by: angshuman
12 Replies

2. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

3. Shell Programming and Scripting

Add row as a column

I have a unix file which has the below format. one row with one column and few rows with 6 columns. I want to add the first row as column to the below rows. Current file br95 sbd 234 567 abc dsb xyz dcb 434 567 abc dsb xyz cbd 434 567 abc dsb xyz 2bd 234 567 abc dsb xyz br75 sbd 234 567... (5 Replies)
Discussion started by: anita81
5 Replies

4. Shell Programming and Scripting

awk - how do i get the last row of a group

How do i print the last record of a group in a file ? For example, I have a file like this : cat txt cucm1,location1,1,2,3 cucm2,location1,3,4,5 cucm1,location1,10,20,30 cucm2,location2,30,40,50 I am expecting a command that would print the last record of the group. For example, ... (2 Replies)
Discussion started by: Lakshmikumari
2 Replies

5. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

6. Shell Programming and Scripting

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. Shell Programming and Scripting

Merge group numbers and add a column containing group names

I have a file in the following format. Groups of data merge together and the group number is indicated above each group. 1 adrf dfgr dfg 2 dfgr dfgr 3 dfef dfr fd 4 fgrt fgr fgg 5 fgrt fgr (3 Replies)
Discussion started by: Lucky Ali
3 Replies

8. Shell Programming and Scripting

Add value of each row when each row has different data ype

Guys -- I am noob and I am really strugging on this one. I cant even write the pseudo code for it. I have a file that spits values in individual rows as such: file: user date type size joe 2005-25-09 log 1 joe 2005-25-09 snd 10 joe 2005-25-09 rcd 12 joe 2005-24-09 log 2 joe... (1 Reply)
Discussion started by: made2last
1 Replies

9. Shell Programming and Scripting

shell script add each row value

Hi, I wrote below small script which will add up each row value from a file and print the total, but it is not.plz let me know what is wrong.script hangs and not displaying anything. ----------------- while read line ; do sum=0; sum=$sum+$line; done echo $sum; exit 0; ------------------... (8 Replies)
Discussion started by: pradeep_script
8 Replies

10. UNIX for Dummies Questions & Answers

Add group

Hi All, How can I add few existing groups into a group? eg. Currently, I have - projgrp1 for proj1 with members proj1usr1, proj1usr2, proj1usr3..proj1usr10 - projgrp2 for proj2 with members proj2usr1, proj2usr2 - projgrp3 for proj3 wiht members proj3usr1, proj3usr3 Now, I would like to... (3 Replies)
Discussion started by: tifmils
3 Replies
Login or Register to Ask a Question