Fellow Monks,
my cubemate (for interesting reasons which are not relevent here) needed some way to make an automated update to his source files (java). the particular edit was to simply add a single blank line to the top of the file. He has a large list of files to do this to.
I was asked if I could think of a solution different than his, which was a vi trigger (that works quite nicely in fact). Being humbled and enlightened by his solution, I felt obligated to share a solution using Perl.
so I set forth to create a perl one-liner to accomplish this goal. Note this is my second attempt to do such a thing...i.e. assist a co-worker not exposed at all in perl by creating a one-liner to solve the problem. (what better way to show off the power of perl, than a neat one-liner?). My first attempt is documented in the Monastery here: Quick solutions to easy problems
while I did not accomplish the goal of the one-liner in the above situation, I did solve the problem. And by posting, I rec'd a few real one-line solutions (many thanks to tilly, merlyn).
In this case, I was close for some time, but could not get this (or some other solutions) to work against multiple files. then I found (RTFM) that <> never does an explicit close (so $. was not being re-initialized) and success was at hand.
So I post my successful one-liner in anticipation of more elegent solutions being posted, as we all know TIMTOWTDI, and those other ways will surely result in a solution that requires a bit less typing ;-)
xdev$ cat foo line 1 line 2 line 3 xdev$ cat foo1 line 1 line 2 line 3 xdev$ perl -i.bak -ne 'if ($.==1){print "\n";}print;if (eof){close ARG +V;}' foo* xdev$ cat foo line 1 line 2 line 3 xdev$ cat foo1 line 1 line 2 line 3 xdev$
In reply to Yet another one-liner by wardk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |