1

I have a problem with removing columns from a file.

input.tsv:

Otu1    otu2    otu3    otu4    otu5
1   2   5   9   3
8   9   8   4   2

I would like to remove a column if its header is listed in file remove.txt, e.g.:

otu2
otu3

So the result would be:

Otu1    otu4    otu5
1   9   3
8   4   2

How can I do this?

1 Answer 1

0

Example using Perl

removeCols.pl:

#!/usr/bin/perl

my $file1="input.tsv";
my $file2="remove.txt";

open RFILE, $file2;
@cols=<RFILE>;

open INPUT, $file1;

#read header line, save indicies
my @header = split( /\t/, <INPUT> );

for my $i (0..$#header){
    if (grep(/$header[$i]/, @cols)){
        push @idx,$i;
        $header[$i] = undef;
    }
}
print join("\t",grep(defined,@header));


# loop remaining file
while(<INPUT>){
    my @line = split(/\t/, $_);
    $line[$_] = undef for (@idx);
    print join("\t",grep(defined,@line));
}

command-line run as such:

prompt> perl removeCols.pl

1
  • Thanks for your help :) I edited my question, I hope it is clearer now Commented Nov 15, 2016 at 14:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.