Impute values within groups


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Impute values within groups
# 8  
Old 12-13-2014
This is probably no better than what you already have, but what the heck!

Code:
#!/bin/bash

# sort -k 1,1 -k 4,4 -o infile impute.data
infil=infile
outfil=outfile
wrkfil=/tmp/impute
touch $wrkfil
touch $outfil
> $wrkfil
> $outfil
fmt="%-10s%-8s%-8s%-14s\n"
typ1=T1
typ2=T2

printf $fmt "Group" "Type" "Value" "Serial_Number" >> $wrkfil
do_dump ()
{
    t1lin=$(grep 'T1 ' $wrkfil)
    t2lin=$(grep ' T2' $wrkfil)
    t1xx="$mix1$mix1"
    t2xx="$mix2$mix2"

    # both T1 and T2 missing
    if [[ ! $t1lin && ! $t2lin ]]; then
        printf $fmt $grp $typ1 $t1xx "impute" >> $outfil
        printf $fmt $grp $typ2 $t2xx "impute" >> $outfil

    # only T2 missing
    elif [[ $t1lin && ! $t2lin ]]; then
        set $t1lin
        [[ ${3:0:1} == $mix2 ]] && t2xx="$mix1$mix1"
        add=$(printf $fmt $grp $typ2 $t2xx "impute")
        sed -i '/T1 /a\'"$add"'' $wrkfil

    # only T1 missing
    elif [[ ! $t1lin && $t2lin ]]; then
        set $t2lin
        [[ ${3:0:1} == $mix1 ]] && t1xx="$mix2$mix2"
        printf $fmt $grp $typ1 $t1xx "impute" >> $outfil
    fi

    # dump to output file
    cat $wrkfil >> $outfil
    > $wrkfil
}

while read -r col1 col2 col3 col4
do
    [[ ${col1:5:1} != [0-9] ]] && continue
    [ ! $grp ] && grp=$col1
    if [[ $col1 == $grp ]]; then
        if [[ ${col3:0:1} != ${col3:1:1} ]]; then
            mix1="${col3:0:1}"
            mix2="${col3:1:1}"
        fi
    else
        do_dump
        grp=$col1
    fi
    printf $fmt $col1 $col2 $col3 $col4 >> $wrkfil
done < $infil
do_dump
cat $outfil

# eof #

# output
# ------
# Group     Type    Value   Serial_Number
# Group1    T1      aa      1             
# Group1    T2      tt      2             
# Group1    T1*T2   tt      3             
# Group1    T1*T2   tt      4             
# Group1    T1*T2   at      5             
# Group4    T1      aa      1             
# Group4    T2      gg      impute        
# Group4    T1*T2   gg      3             
# Group4    T1*T2   aa      4             
# Group4    T1*T2   ag      5             
# Group5    T1      gg      1             
# Group5    T2      tt      impute        
# Group5    T1*T2   gg      3             
# Group5    T1*T2   gt      4             
# Group5    T1*T2   gg      5             
# Group6    T1      aa      impute        
# Group6    T2      gg      2             
# Group6    T1*T2   ag      3             
# Group6    T1*T2   gg      4             
# Group6    T1*T2   ag      5             
# Group6    T1*T2   gg      6             
# Group7    T1      gg      impute        
# Group7    T2      aa      impute        
# Group7    T1*T2   ga      7             
# Group7    T1*T2   gg      8             
# Group7    T1*T2   ga      9             

# real    0m0.034s
# user    0m0.009s
# sys 0m0.016s

This User Gave Thanks to ongoto For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Groups is not visible

OS : SunOS 5.8 I am trying to add a user ad3059 to the following groups, A B C D ( four groups A,B,C,D) When i use usermod command and add the user to the above groups, and go to > groups ad3059 other C D It doesnt show A and B groups and shows it as other.Please advice on how... (13 Replies)
Discussion started by: Revathi2089
13 Replies

2. Shell Programming and Scripting

Print values within groups of lines with awk

Hello to all, I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX". My issue with current code is that should print values for 3 groups and only is... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

3. UNIX for Dummies Questions & Answers

Groups

Must I be in a group? I am using Ubuntu and am the only user on my PC. I know how to change groups but do not see a way to not be in a group. Any help would be appreciated. (2 Replies)
Discussion started by: nthepines
2 Replies

4. Shell Programming and Scripting

Groups in Unix ???

What is Primary group and Secondary Group in Unix.? (1 Reply)
Discussion started by: gwgreen1
1 Replies

5. HP-UX

Groups access

Hi all, Can someone tell me how I can get around this problem. Basically I use the HP-UX OS and I work with 2 top level directories. /z/group1 /z/group2 these 2 dirs are managed where group1 can only be access by one set of users and group2 another. This is managed by adding the 2... (3 Replies)
Discussion started by: cyberfrog
3 Replies

6. Shell Programming and Scripting

Remove matched values and their related groups

For each value in file1 it has to check in file2 and file3. If value matched it has to delete that value and related group value in file2 and file3. In this example it takes A , deletes A and take related group value 1 and deletes E-1,then checks in file3 and deletes K-1.After that it takes D... (7 Replies)
Discussion started by: kanagaraj
7 Replies

7. Solaris

groups

1 user in member of 4 groups find file permissions and default group (1 Reply)
Discussion started by: tirupathi
1 Replies

8. Solaris

groups

how to create 1000 users in 1 group (0 Replies)
Discussion started by: tirupathi
0 Replies

9. Shell Programming and Scripting

groups starting with c2?

I have some groups and when i issue a command like groups $LOGNAME it displays in one line rfautosys c2ru cash2 I want to fetch only group starting with c2 but when i grep i am getting full line. Can someone advise on this please as how i can get output as c2ru? (2 Replies)
Discussion started by: gehlnar
2 Replies

10. AIX

Where are my groups

Hello A couple of weeks ago, I added a user to an AIX 5.3 system. I go to add one today, and it appears that when creating a user in smit, I cannot see any groups. No primary groups No Group set No Admin Groups The /etc/group and etc/secuity/group files seem to be intact. I did... (4 Replies)
Discussion started by: mhenryj
4 Replies
Login or Register to Ask a Question