Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2014
    Posts
    2

    Unanswered: first of group by and comparing within group

    hello, i need script assistance for the following problem

    I have a table like

    name1 group1 value1
    name2 group1 value1
    name1 group2 value1
    name2 group2 value2
    name3 group2 value1

    if the values in the 3rd col are consistent within a group i want to output the first row, if they are inconsistent within the same group , i want to output the first row with a value that says 'missing'

    So my output will look like

    name1 group1 value1
    name1 group2 missing
    Last edited by senhia83; 10-12-14 at 22:19.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Umm... Huh?

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Oct 2014
    Posts
    2
    Sorry somehow the data went missing,

    Input

    name1 group1 value1
    name2 group1 value1
    name1 group2 value1
    name2 group2 value2
    name3 group2 value1

    output

    name1 group1 value1
    name1 group2 missing

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    first_sw="Y"   
    prev_group=""                                  
    prev_name=""                                     
    prev_value=""                                    
    while read name group value                      
    do                                               
    if [ $first_sw = "Y" ]                           
            then                                     
            first_sw="N"                             
            prev_group=$group                        
            prev_value=$value                        
            prev_name=$name                          
    fi                                               
    if [ $group != $prev_group ]                     
            then                                     
            echo $prev_$name $prev_group $prev_value 
            prev_group=$group                        
            prev_name=$name 
            prev_value=$value                         
    fi                                               
    if [ $value != $prev_value ]                     
    then                                             
            prev_value="missing"                     
    fi                                               
    done                                             
    echo $prev_name $prev_group $prev_value
    That was programming 101. What do they teach in schools these days?
    Last edited by kitaman; 10-13-14 at 10:56.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •