Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Doing it twice is probably <em>not</em> your problem. If you managed to just do it once using your basic strategy, it would still take you an hour, right? You probably need to use a different technology or tool. Switching to Perl, as above, might make your code a lot faster (give it a try)</p> <p>But continuing down the path of other posters, the next step might be pipelining. Write a little program that replaces two columns, then run that program twice, simultaneously. The first run swaps out strings in column1 with strings in column2, the next swaps out strings in column2 with strings in column3.</p> <p>Your command line would be like this</p> <pre><code>cat input_file.txt | perl replace.pl replace_file.txt 1 2 | perl replace.pl replace_file.txt 2 3 &gt; completely_replaced.txt </code></pre> <p>And replace.pl would be like this (similar to other solutions)</p> <pre><code>#!/usr/bin/perl -w my $replace_file = $ARGV[0]; my $before_replace_colnum = $ARGV[1] - 1; my $after_replace_colnum = $ARGV[2] - 1; open(REPLACEFILE, $replace_file) || die("couldn't open $replace_file: $!"); my @replace_pairs; # read in the list of things to replace while(&lt;REPLACEFILE&gt;) { chomp(); my @cols = split /\t/, $_; my $to_replace = $cols[$before_replace_colnum]; my $replace_with = $cols[$after_replace_colnum]; push @replace_pairs, [$to_replace, $replace_with]; } # read input from stdin, do swapping while(&lt;STDIN&gt;) { # loop over all replacement strings foreach my $replace_pair (@replace_pairs) { my($to_replace,$replace_with) = @{$replace_pair}; $_ =~ s/${to_replace}/${replace_with}/g; } print STDOUT $_; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload