I'm trying to do a somewhat tricky search and replace in a tab delimited txt, and I'm getting odd results.

I want to check line by line if the contents of the first cell are repeated in the second, and if so, delete the contents of the first cell. Otherwise, the line should be reprinted with no changes.

The script I came up with does what I want it to, but in many lines, the last character of the first cell gets deleted. I don't see how this could be caused by an error in my regex, as it should either leave the whole line unchanged or leave the first cell empty; it's not supposed to mess around within the first cell. The strange thing is, I also tried sed and it produces similar errors. (BTW, I'm using Activeperl 5.10.0 on Win XP.)

Here's my script:

#!/usr/bin/perl use strict; use warnings; open(IN, "<:encoding(UTF-8)", "in.txt") or die "Can't open input file: + $!"; open(OUT, ">:encoding(UTF-8)", "out.txt") or die "Can't open output fi +le: $!"; while (<IN>) { s/([^\t]*)\t([^\t]*)\1([^\t]*)\t/\t$2$1$3\t/; print OUT $_; } close IN; close OUT; print "Replacements done"; <STDIN>;
And here's a snippet of my input file (note: my tabs seem to be converted to 4 spaces by the forum software)
abandon/to abandon/to abandon/to (a child) abandon/to (a road) abandon/to (claim) abandon/to (motion) abandon/to (one's appeal) abandon/to (privilege on document) abandon/to abandon rented premises/to abandon/to steal or abandon/to (personal property) abandon/to vacate or abandon/to (possession or occupation of a lan +d) abandoned/to be abandoned/to be abandoned/to be appeal that is abandoned abandoned/to be application that is abandoned abandoned/to be motion that is abandoned abandoned motor vehicle site abandoned motor vehicle site abandoned orchard abandoned orchard abandonment abandonment abandonment (appeal) abandonment (land) abandonment (mines or minerals)
And the output I'm getting (see lines 3,5,6,7,13 etc. for the errors):
abandon/to abandon/to (a child) abandon/t (a road) abandon/to (claim) abandon/t (motion) abandon/t (one's appeal) abandon/t (privilege on document) abandon abandon rented premises/to steal or abandon/to (personal property) vacate or abandon/to (possession or occupation of a land) abandoned/to be abandoned/to b appeal that is abandoned abandoned/to b application that is abandoned abandoned/to b motion that is abandoned abandoned motor vehicle site abandoned orchard abandonment abandonment (appeal) abandonment (land) abandonment (mines or minerals)
Thanks for any help!
Update: solved.
As AnomalousMonk pointed out, I should have made sure the first matched substring starts at the beginning of the line, i.e. s/^([^\t]*)\t([^\t]*)\1([^\t]*)\t/\t$2$1$3\t/;

In reply to Regex search and replace appears to be broken by elef

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.