Sorry, but your specifications are still unclear.

Only tabs in the fields between quotes should be replaced by commas.

But your example input doesn't have any tabs. You should copy and paste an actual sample of an input file into the <code> tags.

You also haven't specified whether the quotes can be escaped or not, i.e. whether a b "x1    \"    x2" is valid and should result in a b "x1,\",x2".

I couldn't make it work from the code from Haukex because I don't have Regexp::Common

Yes, even you can use CPAN, and also in the worst case the regular expressions generated from Regexp::Common can be printed on a machine that has it installed, and then used on one that doesn't.

Depending on your actual input file format, maybe your solution can be as simple as:

$ cat in.txt a b "x1 x2" c d "x2" e f "x3 x4 x5" $ perl -ple 's/(?<=")([^"]*)(?=")/(my$x=$1)=~s#\t#,#g;$x/ge' in.txt a b "x1,x2" c d "x2" e f "x3,x4,x5" $ perl -ple 's/(?<=")([^"]*)(?=")/$1=~s#\t#,#gr/ge' in.txt a b "x1,x2" c d "x2" e f "x3,x4,x5"

The second example only works on Perl 5.14+ due to the /r modifier.


In reply to Re^2: How to substitute all tabs only in a specific field by haukex
in thread How to substitute all tabs only in a specific field by xuo

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.