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}' File
Now, here's another option for you below:perl -lne 'push @a,$_;}{ print join "|",@a' File
The 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.
.png)







I am a software programmer working in India. I usually like to blog in my free time.
0 comments: