Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 24th, 2008, 01:47 AM
sridevikarthik sridevikarthik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 16 sridevikarthik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 50 sec
Reputation Power: 0
Question Appending data in the start of the file

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

Reply With Quote
  #2  
Old April 24th, 2008, 02:13 AM
kalyanraj's Avatar
kalyanraj kalyanraj is offline
8 Miles
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: India
Posts: 286 kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 19 h 50 m 34 sec
Reputation Power: 51
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.

Reply With Quote
  #3  
Old April 24th, 2008, 03:52 AM
sridevikarthik sridevikarthik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 16 sridevikarthik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 50 sec
Reputation Power: 0
#!/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.

Reply With Quote
  #4  
Old April 24th, 2008, 04:29 AM
kalyanraj's Avatar
kalyanraj kalyanraj is offline
8 Miles
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: India
Posts: 286 kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 19 h 50 m 34 sec
Reputation Power: 51
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.

Reply With Quote
  #5  
Old April 24th, 2008, 08:28 AM
vb.net vb.net is offline
Demonic Swordsman DGQB
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Posts: 974 vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 7 h 46 m 20 sec
Reputation Power: 76
Quote:
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


Try Inplace Edit or edit-in-place.

Example of the syntax from a previous post:

perl Code:
Original - perl Code
  1. # put this in a block so that the changes to global variables don't affect other bits of my program
  2. {
  3.     # load the filename into @ARGV so it can be edited in-place
  4.     local @ARGV = ( $file );
  5.  
  6.     # turn on in-place editing
  7.     local $^I = 1;
  8.  
  9.     while (<>)
  10.     {
  11.         if ($. == 1)    #-- first line of the file
  12.         {
  13.             #-- insert the new lines
  14.             print "New line 1\n";
  15.             print "New line 2\n";
  16.         }
  17.  
  18.         # print the old line
  19.         print;
  20.     }
  21. }

Reply With Quote
  #6  
Old April 25th, 2008, 02:13 AM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 1,966 KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level)KevinADC User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 3 h 4 m 48 sec
Reputation Power: 338
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Appending data in the start of the file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway