Perhaps part of the confusion is that runnig's approach is to make a new copy of the file, adding some lines to the output stream at the appropriate places and then rename the new file to the original file name. So, the original file is not being edited in place, but a wholly new copy of the data is being made into a different file. When the new file is renamed to the orignal file name, it "becomes" the original file and the contents of the original file are automatically deleted (or, if you use the -i command line switch, the old file is renamed to another name so you can go back to it if necessary.)
One nice property of this approach is that if your script bombs in the middle, you still have the original contents around. So, I hope this explains why you don't have to "reserve" space in your original file for the added content.
On the other hand, if you want your original file to actually look like:
<---Start of the CSVFILE ----------------------------->
<--Here goes a image --------------------------------->
<--Here goes a blank row ----------------------------->
<--Here goes the header ------------------------------>
123456 123 john 9393939 data data
123456 123 john 9393939 data data
123456 123 john 9393939 data data
123456 123 john 9393939 data data
123456 123 john 9393939 data data
and you want the script to replace
<--Here goes a image --------------------------------->
with something, then that's a different problem, but you can still use the same idea:
while (<>) {
if (m/^<--Here goes a image.../) {
print ...whatever...
} else {
print;
}
}
Use the above code with the
-i option to use perl's file editing feature. If the rows you want to replace are blank, then just use line numbers as the matching criteria:
while (<>) {
if ($. == 2) {
print ...image csv line...
} else {
print;
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.