So, I've done Preserving empty fields in splitsome searching (Preserving empty fields in split) and I understand the various options to split() to include trailing empty fields. However, we have some scripts that have a number of perl one-liners to do some quick record scrubbing and they use -F to split on commas. I can not however find a way to use -F and have it keep the empty trailing fields (without appending some nonsense character to the end of the record and then having to discard it afterwards).

As an example, this loses all the trailing, empty fields:

head -1 file.txt | perl -F, -lane 'print join(",",@F);'

This keeps them, but I have to do extra work to keep the fields:

head -1 file.txt | sed -e 's/$/x/' | perl -F, -lane 'print join(",",@ +F[0..$#F-1]);'

Is there a modifier to this option that will let it do the same thing split does with the extra argument? I'm pretty sure this could be cleaner and clearer.

---
s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.


In reply to -F and trailing empty fields by tuxz0r

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.