0

I have a tab-delimited .txt file that has the format of:

chrI    0   1   -4.231
chrI    1   2   -3.257
chrI    2   3   -2.653
chrI    3   4   -2.102
chrI    4   5   -1.685
chrI    5   6   -1.331
chrI    6   7   -1.036
chrI    7   8   -0.728
chrI    8   9   -0.527
chrI    9   10  -0.401

Is there any chance I can get text wrangler to replace all of the entries in column 2 with the columnn 3 values such that column 3 is essentially duplicated?

Such as this:

chrI    1   1   -4.231
chrI    2   2   -3.257
chrI    3   3   -2.653
chrI    4   4   -2.102
chrI    5   5   -1.685
chrI    6   6   -1.331
chrI    7   7   -1.036
chrI    8   8   -0.728
chrI    9   9   -0.527
chrI    10  10  -0.401

Is there a way to do this in Perl?

fugu
  • 6,417
  • 5
  • 40
  • 75
user3012926
  • 117
  • 1
  • 9

3 Answers3

1

From the command line:

perl -lane 'BEGIN{$"="\t"}$F[2]=$F[1];print "@F"' File.txt [>outFile]

The last, optional parameter directs output to a file.

Output:

chrI    0   0   -4.231
chrI    1   1   -3.257
chrI    2   2   -2.653
chrI    3   3   -2.102
chrI    4   4   -1.685
chrI    5   5   -1.331
chrI    6   6   -1.036
chrI    7   7   -0.728
chrI    8   8   -0.527
chrI    9   9   -0.401

Or as a script (same output):

use strict;
use warnings;

$" = "\t";

while (<>) {
    my @F = split;
    $F[2] = $F[1];
    print "@F\n";
}

Command-line usage: perl script.pl File.txt [>outFile]

Hope this helps!

Kenosis
  • 6,196
  • 1
  • 16
  • 16
0

In perl:

while (<$input>){
    chomp;
    my @split = split(/\t/);
    print "$split[0]\t$split[1]\t$split[1]\t$split[3]\n";
}
fugu
  • 6,417
  • 5
  • 40
  • 75
0

In TextWrangler search for ^(\w+)\s+(\w+)\s+(\w+)\s+ and replace with \1\t\3\t\3\t with find's "Matching:" "Grep" option enabled. (Taking it easy - using tabs for alignment rather than differing number of blanks…)

But if you need the blanks, replace ^(\w+\s+)(\w+\s+)(\w+\s+) with \1\3\3… - Works at least for the sample data.

Abecee
  • 2,365
  • 2
  • 12
  • 20