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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |