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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.