开发者

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开发者_Python百科 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜