Adding per group and selecting consensus name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding per group and selecting consensus name
# 1  
Old 09-10-2015
Adding per group and selecting consensus name

Hello gurus,

I am having trouble selecting consensus name, if any, for each group in my data.

The rules are

1. A group is defined by the first 2 columns.
2. The last column defines the name of the row within a group. If all rows within a group have the same name , we select that name as the name of the group. If the name varies for the different rows in the group, then we cant assign a name and say "None". If the name says "Non-determinate", we can ignore that name.

3. Add columns 3,4 and 5 per group.


Sample input

Code:
V1	V2	1	2	3	LID
V1	V2	4	5	6	LID
V1	V2	7	8	9	Non-determinate
V2	V3	0	0	1	HID
V2	V3	1	0	0	LID
V2	V3	0	1	0	Non-determinate
V1	V3	1	1	1	HID
V1	V3	2	2	2	HID
V1	V3	3	3	3	HID
V1	V3	4	4	4	HID

Sample output

Code:
V1	V2	12	15	18	LID
V2	V3	1	1	1	None
V1	V3	10	10	10	HID

The adding part , I could do, not sure how to do the naming part. please assist

Code:
awk '{ arr1[$1"\t"$2]+=$3; arr2[$1"\t"$2]+=$4;arr3[$1"\t"$2]+=$5;} END {for (i in arr1) print i, arr1[i],arr2[i],arr3[i]}' file

# 2  
Old 09-10-2015
An awk approach:-
Code:
awk -F'\t' '
        {
                I = $1 FS $2
                for ( i = 3; i <= 5; i++ )
                {
                        if ( I in R )
                        {
                                if ( R[I] != $NF && $NF != "Non-determinate" )
                                        R[I] = "None"
                        }
                        else
                                R[I] = $NF

                        A[I FS i] += $i
                }
        }
        END {
                for ( k in R )
                {
                        printf "%s\t", k
                        for ( i = 3; i <= 5; i++ )
                                printf "%d\t", A[k FS i]
                        printf "%s\n", R[k]
                }
        }
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Adding UNIX user to a group

Hi, I am new to unix. I am facing access permission issue I want to access path /app/compress from a user "test" but getting permission denied error This path exist in "Main" user So after some googling i came to know we need to add "test" user in "main" group so path /app/compress ... (7 Replies)
Discussion started by: sv0081493
7 Replies

2. AIX

Adding a Volume Group to an HACMP Resource Group?

Hi, I have a 2 node Cluster. Which is working in active/passive mode (i.e Node#1 is running and when it goes down the Node#2 takes over) Now there's this requirement that we need a mount point say /test that should be available in active node #1 and when node #1 goes down and node#2 takes... (6 Replies)
Discussion started by: aixromeo
6 Replies

3. Shell Programming and Scripting

Adding a user to a group

Hello guys!! If a user is already created on a server, how do you add them to another group? The useradd command? If so then would that duplicate the user account on the server? Thanks Bigben (4 Replies)
Discussion started by: bigben1220
4 Replies

4. Solaris

Adding User to group

Hi all, I have a existing user user1 its group id dba i have created a new user named: uta and added to group dba my task for creating uta ( to ftp solaris server from /oracle/pcmia/dry1 & oracle/pcmia/dry2 and get some rdf ( database patch) and saved in one windows folder named d:\patch... (2 Replies)
Discussion started by: saurabh84g
2 Replies

5. UNIX for Advanced & Expert Users

Adding quota for a group

***deleted by reborg for rule 1 violation*** (1 Reply)
Discussion started by: manoranjan
1 Replies

6. UNIX for Dummies Questions & Answers

Adding users to /etc/group

I'm using SAM to add users on an HP and they're adding fine. But in /etc/group it only lists the group names. It's not adding the users in there. Is there a way to have them put in there without going into SAM and modifying the group and adding them? I guess what I want to happen is when I add... (1 Reply)
Discussion started by: golfhakker
1 Replies

7. UNIX for Dummies Questions & Answers

Adding root user to a group

Hey everyone, I need a little help.... I need to add my root user to a new group I have created, I'm just alittle unsure how to do this. I know I need to use the 'useradd' command, the user 'root' needs to be added to a new group called 'beoper' and retain his membership in the following... (7 Replies)
Discussion started by: The Specialist
7 Replies

8. UNIX for Dummies Questions & Answers

Adding an extension to a group of filenames

Hi - I'm stuck. I have a group of text files created using the split command. My files have the names "projectaa", "projectab", "projectac", etc. What I want to do is add the extension ".txt" to each file. I think I've got part of a sed command together, but I'm stuck on my regex - I keep getting... (9 Replies)
Discussion started by: pepintheshort
9 Replies

9. UNIX for Dummies Questions & Answers

Adding a user to a group

Now, its been a while since i done this but I had to add a user to a group. I did that by using the usermod command and now when I superuser to the user's account and issue a "id", i get the desired gid. i mean, output of id indicated the user is assigned to the group i want him to be in. ... (5 Replies)
Discussion started by: TRUEST
5 Replies
Login or Register to Ask a Question