Data filtering and category assigning


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Data filtering and category assigning
# 1  
Old 12-05-2014
Data filtering and category assigning

Please consider the following file, I have many groups which can be of 3 types, T1 (Serial_Number 1) T2 (Serial_Number 2) and T1*T2 (all other Serial_Number).

I want to only consider groups that have both T1 and T2 present and their values are different from each other. In the example file, Group3 and Group 5 are not to be considered for the same reasons.
Important to mention that the data is not sorted, so T1, T2 and T1*T2 rows are scattered in the file, in no particular order.



Code:
Group	Type	Value	Serial_number
Group1	T1	aa	1
Group1	T2	tt	2
Group1	T1*T2	at	3
Group1	T1*T2	tt	4
Group2	T1	gg	1
Group2	T2	tt	2
Group2	T1*T2	gg	3
Group2	T1*T2	tt	4
Group2	T1*T2	gt	5
Group3	T1	gg	1
Group3	T2	gg	2
Group3	T1*T2	gg	3
Group3	T1*T2	gg	5
Group4	T1	gg	1
Group4	T2	tt	2
Group4	T1*T2	gt	4
Group4	T1*T2	gg	5
Group5	T1	gg	1
Group5	T1*T2	gt	5


I want to add a column to the output , only for types T1*T2 that states if they match the corrsponding value of T1 in the group, or T2 in the group or doesnt match any of T1 or T2.


For example for Group1, the value of T1*T2 (Serial_number 3) is 'at' which
doesnt match its T1 value of 'aa' or T2 value of 'tt'. So it is 'different'

For Group1, the value of T1*T2 (Serial_number 4) is 'tt' which matches T2 value of 'tt' , so it assigned 'T2-like'

Code:
Group	Type	Value	Serial_number	Similar_To
Group1	T1*T2	at	3	different
Group1	T1*T2	tt	4	T2-like
Group2	T1*T2	gg	3	T1-like
Group2	T1*T2	tt	4	T2-like
Group2	T1*T2	gt	5	different
Group4	T1*T2	gt	4	different
Group4	T1*T2	gg	5	T1-like

This is my feeble attempt, which doesn't work.

Code:
awk 
' {
   if(!($1 in grp)) {
      grp[$1]++
      type[$1]=$2
      val[$1,1]=$3 FS $4
      next
   }
  NR != 1 {
 grp[$1]++
 type[$2]++
 val[$3]++
 a[$1,$2]=a[$1,$2]" "$3
 
 if($3=="T1") categ="T1-like"
 else if ($3=="P2") categ="T2-like"
 else categ="different"
 
 if($3="T1*T2")
   for (i=1;i<length(grp);i++)
   	if (grp[i]==grp[i-1])
   		catg1[i]=categ
   print $1 FS $2 FS $3 FS $4 FS catg1[i]
 
 }' infile


Last edited by jianp83; 12-05-2014 at 02:31 PM..
# 2  
Old 12-05-2014
Strange, complex conditions ... there might be a more elegant solution, but try
Code:
awk     'NR==1          {print $0, "similar to"}
         NR==FNR        {if ($2 !~/\*/) {L[$1,$2]=$3
                                         G[$1]++
                                         T[$2]}
                         next}
         $2 ~ /\*/      {C=0
                         K="diff"
                         for (i in T) if (L[$1,i] == $3) {C++; K=i}
                         if (C<2 && G[$1]>=2) print $0, K  
                        }
        ' SUBSEP="," OFS="\t" file4 file4
Group    Type    Value    Serial_number    similar to
Group1    T1*T2    at    3    diff
Group1    T1*T2    tt    4    T2
Group2    T1*T2    gg    3    T1
Group2    T1*T2    tt    4    T2
Group2    T1*T2    gt    5    diff
Group4    T1*T2    gt    4    diff
Group4    T1*T2    gg    5    T1

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-08-2014
Thank you, my only concern is with reading these data files twice. They can be upto 25Gbs in the future, right now, they are in manageable range.
# 4  
Old 12-11-2014
Is you input file (other than the header line) always in sorted order with the Group (field 1) being the primary sort key and the Serial Number (field 4) being the secondary sort key (as in your sample input file)?

When present, can the Type T1 and T2 Values be anything other than "aa", "gg", or "tt"?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-16-2014
Quote:
Originally Posted by Don Cragun
Is you input file (other than the header line) always in sorted order with the Group (field 1) being the primary sort key and the Serial Number (field 4) being the secondary sort key (as in your sample input file)?

When present, can the Type T1 and T2 Values be anything other than "aa", "gg", or "tt"?
Don, they are sorted
Code:
-k1,1 -k4,4

.

T1 and T2 values can be among the set of 6 : aa,gg,tt,cc,dd,ii

Important to note that the groups with T1 and T2 values equal and groups with either/both T1 and T2 being absent must be ignored,please consider that as a preprocessing filter if you will.

Thanks for helping !
# 6  
Old 12-17-2014
Could this help you ?
script name - Forum1.pl
Code:
#!/usr/bin/perl

my %hashTree;
my @array;

while (<>) {
  chomp;
  @array=split;
  $hashTree{$array[0]}{$array[3]}=[$array[1] ,$array[2]];
}

my ($hashTreeRef,$groupName,$srNo,$status,$groupName);

foreach ( keys %hashTree ) {
  undef $hashTreeRef;
  undef $groupName;

  $hashTreeRef=$hashTree{$_};
  $groupName=$_;

  undef $srNo;
  undef $status;
  undef $vT1;
  undef $vT2;
  undef $vT1T2;

  foreach ( sort { $a <=> $b} keys %$hashTreeRef ) {
    $srNo=$_;
    if($$hashTreeRef{$_}[0] eq "T1") { $vT1=$$hashTreeRef{$_}[1]; }
    if($$hashTreeRef{$_}[0] eq "T2") { $vT2=$$hashTreeRef{$_}[1]; }
    if($$hashTreeRef{$_}[0] eq 'T1*T2') {
      if ( ( defined ($vT1) and defined ($vT2) ) and $vT1 ne $vT2 ) {
          $vT1T2=$$hashTreeRef{$_}[1];
          $status= $vT1T2 eq $vT1 ? "T1-like" : $vT1T2 eq $vT2 ? "T2-like" : "different";
          printf "$groupName\t$$hashTreeRef{$_}[0]\t$vT1T2\t$srNo\t$status\n";
      }
    }
  }
}

Invocation
Code:
perl Forum1.pl infile

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 12-17-2014
Hello Praveen, thank you for the solution. However there might be some problem. The number of rows returned is much less than what is in the input



Code:
 $ cat prav
Group77 T1      AA      Snum048PX-02052
Group77 T2      GG      Snum060PX-02118
Group77 T1*T2   AA      Snum046PX-03072
Group77 T1*T2   AA      Snum048PX-00003
Group77 T1*T2   AA      Snum048PX-00008
Group77 T1*T2   AA      Snum048PX-00010
Group77 T1*T2   AA      Snum048PX-00014
Group77 T1*T2   AA      Snum048PX-00015
Group77 T1*T2   AA      Snum048PX-00016
Group77 T1*T2   AA      Snum048PX-00019

perl prav.pl prav
Group77 T1*T2   AA      Snum048PX-00014 T1-like
Group77 T1*T2   AA      Snum048PX-00008 T1-like
Group77 T1*T2   AA      Snum048PX-00016 T1-like
Group77 T1*T2   AA      Snum048PX-00003 T1-like

Hello Rudi,

For the following set of data no output should be produced since T1 and T2 values are same. Would you please look into this problem?

Code:
$ cat  rudi
Group78 T1      AA      Snum048PX02065
Group78 T2      AA      Snum060PX02052
Group78 T1*T2   AA      Snum048PX02068
Group78 T1*T2   AA      Snum048PX02069
Group78 T1*T2   AA      Snum048PX02070
Group78 T1*T2   AG      Snum048PX02093


$ awk     'NR==FNR        {if ($2 !~/\*/) {L[$1,$2]=$3
>                                          G[$1]++
>                                          T[$2]}
>                          next}
>          $2 ~ /\*/      {C=0
>                          K="diff"
>                          for (i in T) if (L[$1,i] == $3) {C++; K=i}
>                          if (C<2 && G[$1]>=2) print $0, K
>                         }
>         ' SUBSEP="," OFS="\t" rudi rudi 
Group78 T1*T2   AG      Snum048PX02093  diff

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting column data based on category assignment

please help with the following. I have 4 col data .. instrument , category, variable and value. the instruments belong to particular categories and they all measure some variables (var1 and var2 in this example), the last column is the value an instrument outputs for a variable. I have used... (0 Replies)
Discussion started by: ritakadm
0 Replies

2. Shell Programming and Scripting

Need help Filtering Data from an API

Hi Everyone, I need help on figuring out a way to filter some data that I get back from an API. Im able to get all the data that Im looking for but I would like to know a way for me to filter it better. The data that Im getting back is basically 2 rows of data as seen here. Row 1 ... (25 Replies)
Discussion started by: TheStruggle
25 Replies

3. Shell Programming and Scripting

Filtering out the data with dates

Hi, I have some data like seen below. format : apple(hhmm mm/dd).fruit apple(2345 03/25).fruit apple(2345 05/06).fruit orange(0443 05/02).fruit orange(0345 05/05).fruit orange(2134 05/04).fruit grape(0930 04/24).fruit grape(2330 03/30).fruit I need to get the data which are... (1 Reply)
Discussion started by: jayadanabalan
1 Replies

4. Shell Programming and Scripting

awk data filtering

I am trying to filter out some data with awk. If someone could help me that would be great. Below is my input file. Date: 10-JUN-12 12:00:00 B 0: 00 00 00 00 10 00 16 28 B 120: 00 00 00 39 53 32 86 29 Date: 10-JUN-12 12:00:10 B 0: 00 00 00 00 10 01 11 22 B 120: 00 00 00 29 23 32 16 29... (5 Replies)
Discussion started by: thibodc
5 Replies

5. Shell Programming and Scripting

Filtering data using AWK

Hi , i have file with delimiter as "|" and data in Double codes for all fields. how to filter data in a column like awk -F"|" '$1="asdf" {print $0}' test. ex : "asdf"|"zxcv" Thanks, Soma (1 Reply)
Discussion started by: challamsomu
1 Replies

6. Shell Programming and Scripting

Parsing out the first (top) data lines of each category

Hi All, I need some help in parsing out the first (top) data lines of each category (categories are based on the first column a, b, c, d, e.( see example file below) from a big file a dfg 3 6 8 9 a fgh 5 7 0 9 a gkl 5 2 4 7 a glo 7 0 1 5 b ghj 9 0 4 2 b mkl 7 8 0 5 b jkl 9 0 4 5 c jkl 2... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. Shell Programming and Scripting

help need in filtering data

Hello Gurus, Please help me out of the problem. I ve a input file as below input clock; input a; //reset all input b; //input comment output c; output d; output e; input f; //output comment I need the output as follows: \\Inputs (1 Reply)
Discussion started by: user_prady
1 Replies

8. UNIX for Dummies Questions & Answers

Filtering Data

file1 contain: (this just a small sample of data it may have thousand of lines) 1 aaa 1/01/1975 delhi 2 bbb 2/03/1977 mumbai 3 ccc 1/01/1975 mumbai 4 ddd 2/03/1977 chennai 5 aaa 1/01/1975 kolkatta 6 bbb 2/03/1977 bangalore program: nawk '{ idx= $2 SUBSEP $3 arr = (idx in arr) ?... (2 Replies)
Discussion started by: bobo
2 Replies

9. Shell Programming and Scripting

Filtering Data

Hi All, I have the below input and expected ouput. I need a code which can scan through this input file and if the number in column1 is more than 1 , it will print out the whole line, else it will output "No Re-occurrence". Can anybody help ? Input: 1 vvvvv 20 7 7 23 0 64 6 zzzzzz 11 5... (7 Replies)
Discussion started by: Raynon
7 Replies

10. UNIX for Dummies Questions & Answers

Filtering out data ...

I have following command which tells me File size in GBs which are greater than 0.01GBs recursively in a dir structure. ls -l -R | awk '{ if ($5/1073741824 >= 0.01) print $9, $5/1073741824 }' But there are some files whom I dont have enough permissions, after executing this script gives me... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question