1

I have 2 files ("data.tab" and "mylist.tab").

my "data.tab" file looks like:

Info_1    abc1     abc2     abc3
Info_2    abc5     ghi4
Info_3    abc10
Info_4    abc8     abc7     abc87    klm78    abc99
Info_5    

my "mylist.tab" file looks like this:

abc2
abc10
abc34
abc99
abc78
abc8
abc3
abc5
abc4

I would like to search if each string in "data.tab" (except column 1) is present in "mylist.tab". I would like to create a third file "output.tab" where if the string is present, then I would include a "1", else "0" in that respective row/column.

Example: "output.tab"

Info_1   0    1     1
Info_2   1    0
Info_3   1
Info_4   1    0     0    0    1
Info_5   

Please note that "data.tab" contains different number of columns for each row.

4 Answers 4

3

This is straightforwardly expressed in awk:

awk 'FNR==NR { h[$1]; next } { for(i=2; i<=NF; i++) $i = ($i in h)? 1 : 0 } 1' mylist.tab data.tab

Or in a more readable format:

parse.awk

# Collect mylist.tab into the `h` associative array
FNR==NR {
  h[$1]
  next
}

# For all but the first column in data.tab check and record if it is in `h`
{ 
  for(i=2; i<=NF; i++) 
    $i = ($i in h) ? 1 : 0 
}

# Short for { print $0 }
1

Run it like this:

awk -f parse.awk mylist.tab data.tab

Output:

Info_1 0 1 1
Info_2 1 0
Info_3 1
Info_4 1 0 0 0 1
Info_5

Or for tab-delimited columns:

awk -v OFS='\t' -f parse.awk mylist.tab data.tab

Output:

Info_1  0   1   1
Info_2  1   0
Info_3  1
Info_4  1   0   0   0   1
Info_5
2

Perl to the rescue!

Save the list elements to a hash, then read the table, split on whitespace and check the hash to print either 0 or 1.

#!/usr/bin/perl
use warnings;
use strict;

my %in_list;
open my $LIST, '<', 'mylist.tab' or die $!;
while (<$LIST>) {
    chomp;
    $in_list{$_} = 1;
}

open my $TAB, '<', 'data.tab';
while (<$TAB>) {
    my @cells = split;
    print shift @cells, "\t";
    print join "\t", map $in_list{$_} ? 1 : 0, @cells;
    print "\n";
}
1
  • That works like a charm! I actually didnt think in terms of perl!! :-) Thank you!!
    – biobudhan
    Commented Oct 25, 2016 at 13:08
2

Use sed to create a sed script from mylist.tab and run it on data.tab:

sed \
    -e '1i s/^[ \\t]*//' \
    -e 's@\(.*\)@s/\\([ \\t]\\)\1\\b/\\11/@g' \
    -e '$as/\\([ \\t]\\)[^ \\t]\\{2,\\}\\b/\\10/g' mylist.tab \
    > /tmp/x.sed 
sed -f /tmp/x.sed data.tab

Note I assume that all strings in "mylist.tab" have at least 2 characters.

1

Another perl solution

$ perl -lne 'if(!$#ARGV){ $h{$_}=1 }
             else{ s/\h\K\H+/$h{$&} ? 1 : 0/ge; print }
            ' mylist.tab data.tab
Info_1    0     1     1
Info_2    1     0
Info_3    1
Info_4    1     0     0    0    1
Info_5    
  • if(!$#ARGV){ $h{$_}=1 } build a hash of words in mylist.tab
  • s/\h\K\H+/$h{$&} ? 1 : 0/ge for lines in data.tab, replace with 1 if present in hash variable, else 0. The \h\K is positive lookbehind for presence of blank, thus avoiding first column from matching
  • Then print the modified line

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .