|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
How to append data in the start of the file.
If i try to append using ">>" it gets appened in the last line of the file. I need my data to be appened in the first as well as in the last line of the file. How do i do it using Perl |
|
#2
|
||||
|
||||
|
Here is some idea of how to do. But this uses 2 files to do the task..
Code:
#!/usr/bin/perl -w;
open (NEWFILE, ">your new file's name") or die "Couldn't open new file: $!";
print NEWFILE $new_data;
# ... and so on ...
open (OLDFILE, "<your old file's name") or die "Couldn't open old file: $!";
while (chomp ($line = <OLDFILE>)) {
print NEWFILE "$line\n";
}
close OLDFILE;
close NEWFILE;
# And now, if you want: rename "your new file's name", "your old file's name";
another go ... you can find some idea... Append at the beginning HTH Raj Last edited by kalyanraj : April 24th, 2008 at 02:19 AM. |
|
#3
|
|||
|
|||
|
#!/usr/bin/perl
#!/usr/bin/perl -w; my $new_data="starting"; open (NEWFILE, ">>C:\\new.txt") or die "Couldn't open new file: $!"; print NEWFILE $new_data; open (OLDFILE, "<C:\\old.txt") or die "Couldn't open old file: $!"; while (chomp ($line = <new.txt>)) { print NEWFILE "$line\n"; } close OLDFILE; close NEWFILE; this what i have tried...but it is not working the way what i expect..instead it gets appened only in the end and in the Start. |
|
#4
|
||||
|
||||
|
I assume with your code posted that you don't have enough knowledge on Perl and simply pasting the code you've got over internet.
Please check the link in the earlier post and modify according to your requirement. In your code: Code:
while (chomp ($line = <new.txt>)) {
Can you tell me what exactly you're trying to do in the above... No hard feelings please. |
|
#5
|
||||||
|
||||||
|
Quote:
Try Inplace Edit or edit-in-place. Example of the syntax from a previous post: perl Code:
|
|
#6
|
||||
|
||||
|
expanding on vb.nets code, which is also the way I would do it:
Code:
# put this in a block so that the changes to global variables don't affect other bits of my program
{
# load the filename into @ARGV so it can be edited in-place
local @ARGV = ( "file.txt" );
# turn on in-place editing
local $^I = '.bac';
while (<>) {
print "New Line\n" if $. == 1;
if (eof) { # add the new line to the end also
print;
print "\nNew Line\n";
}
else {
print;
}
}
}
Note the double newlines here: Code:
print "\nNew Line\n"; If you know the last line of the file has a newline on the end you can drop the first newline. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Appending data in the start of the file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|