3

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?

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
  • Perl users like to say "a correct way" rather than "the correct way", but your little script looks fine. – mob Feb 17 '11 at 16:46

1 Answers1

3

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.

daxim
  • 39,270
  • 4
  • 65
  • 132