Butterfly in Perl command line "}{"
I recently came to know about this and I thought its worth sharing. I will try to keep it very simple.Lets say I have a file as below:
1 2 3 4 5I need to join all the lines with a pipe so that my output should look like below:
1|2|3|4|5Normally I use the below command to achieve the same:
perl -lne 'push @a,$_;END{print join "|",@a}' FileNow, here's another option for you below:
perl -lne 'push @a,$_;}{ print join "|",@a' FileThe change here is:
}{
This is called butterfly option in perl. Basically it closes while loop imposed by -n switch, and what follows }{ is block executed after while loop.
0 comments: