Search code examples
perlinplace-editing

Perl's autosplit function with in place editing


I just had a task in where I needed to replace each 3rd value in a tabulator separated file with a fixed value. I guess it can be done in Perl on a Unix shell like so

$perl -a -n -i  -F'/\t/' -e '$F[2]="THE FIXED VALUE";print join "\t", @F' bla.txt

I just wanted to know if this is a "correct" way, or if there is a better (for a currently lacking definition of better) to do it?


Solution

  • I think your one-liner is reasonable and readable. There are many more ways to do it. I would stack the perlrun options and save a few keystrokes:

    perl -F'\t' -i -ape'$F[2]="THE FIXED VALUE"; $_ = join "\t", @F' bla.txt
    

    A shame that $, does not get populated with the argument of -F, so there's still a piece of repetition.