473,654 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write to line1 of an exisitng file in perl

154 New Member
Hi I am trying to write a line to a file but i want it to write to line 1 of the file and push down what was on line 1.

The code below works but inserts the line to the bottom of the page and i need the line to append to line1.

Could somebody help me out on how to do do this please.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $| = 1;
  3.  
  4. opendir(DIR, ".") or die $!;
  5.  
  6. @files = readdir(DIR),"\n";
  7. close(DIR);   
  8.  
  9. my $i = 1;    
  10.  
  11. foreach (@files) {
  12.  
  13.  
  14.    @_ = open (APPEND, ">>$_") or die "$! error trying to append";
  15.    print APPEND "MATERIAL_UOM|MATERIAL_NUM|MATERIAL_DESC|X_PLANT";
  16.  
  17.  $i++;     
  18.  print "\n\n $_ Job is finished\n\n";  
  19.  
  20. };
Jan 20 '07 #1
6 2606
KevinADC
4,059 Recognized Expert Specialist
like so much of perl, there is more than one way to do this, but using perls inplace editor makes this job quite easy.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/!perl
  2. opendir(DIR, ".") or die $!;
  3. my @files = readdir DIR;
  4. close(DIR);   
  5. {
  6.    local @ARGV = @files;
  7.    local $^I = '.bac';
  8.    while (<>) {
  9.       if ($. == 1) {  
  10.          print "MATERIAL_UOM|MATERIAL_NUM|MATERIAL_DESC|X_PLANT\n"; 
  11.       }
  12.       print;
  13.    }
  14. }
  15. print "all finished";
  16.  

my concern though is these two lines:

Expand|Select|Wrap|Line Numbers
  1. opendir(DIR, ".") or die $!;
  2. my @files = readdir DIR;
"." will be the current directory and reading all the files in the directory into the @files array might not be what you want to do. Use the above code at your own risk if you run it as-is. Because it will attempt to open every file in the array and prepend the new line into the file. If that is really what you want to do then fine, but make sure to run some tests on dummy files anyway so you are sure it works like you want.
Jan 20 '07 #2
jonathan184
154 New Member
Thank you for the script i am going to try this soon.

I just got one question
local $^I = '.bac'; does this mean it will add lines to *.bac files alone in the array?

about the read dir I put the . for testing when everything works good i will put the full path and the script in a different dir.

Thanks for your reply i really appreciate it.
Jan 20 '07 #3
KevinADC
4,059 Recognized Expert Specialist
I just got one question
local $^I = '.bac'; does this mean it will add lines to *.bac files alone in the array?
No. That line tells perl to make a backup copy of the original file and name the file with a .bac extension (you can choose any name for the extension). If the original were "log.txt" perl will make a backup copy of the original file named "log.txt.ba c". If you're using a nix box and don't want a backup copy you can use:

Expand|Select|Wrap|Line Numbers
  1. $^I = '';
but if you are using a windows box you most likely will have to define the backup file extension otherwise windows throws an error and the script dies, but you can delete the backup file after editing the original file.
Jan 20 '07 #4
KevinADC
4,059 Recognized Expert Specialist
btw,

assigning the return value of the open function to the system array @_ isn't really doing anything.

Expand|Select|Wrap|Line Numbers
  1. @_ = open (APPEND, ">>$_") or die "$! error trying to append";

better written like this:

Expand|Select|Wrap|Line Numbers
  1. open (APPEND, ">>test.txt") or die "$! error trying to append";
  2. print APPEND "MATERIAL_UOM|MATERIAL_NUM|MATERIAL_DESC|X_PLANT\n";
  3. close(APPEND);
  4.  
unless you were really planning on doing something with @_, but I can't imagine what?
Jan 20 '07 #5
jonathan184
154 New Member
When i run the script below i get an error, now i have the script at d:\\Documents and Settings\\peewe e2\\Desktop\\lo ad these\\jan18

and the files that need to have the line added is at d:\\Documents and Settings\\peewe e2\\Desktop\\lo ad these\\jan18\\b ackup\\

but when i run the script I got this message:

Can't open int_sap_contrac tcontact_180107 _999082.txt: No such file or directory
at addline1.pl line 8.
Can't open int_sap_contrac tcontact_180107 _999082.txt.bac : No such file or direct
ory at addline1.pl line 8.



When i checked line8 this is what i have on line 8
Expand|Select|Wrap|Line Numbers
  1. while (<>) {
Where do you think iam going wrong?


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/!perl
  2. opendir(DIR, "d:\\Documents and Settings\\peewee2\\Desktop\\load these\\jan18\\backup\\") or die $!;
  3. my @files = readdir DIR;
  4. close(DIR);   
  5. {
  6.    local @ARGV = @files;
  7.    local $^I = '.bac';
  8.    while (<>) {
  9.       if ($. == 1) {  
  10.          print "MATERIAL_UOM|MATERIAL_NUM|MATERIAL_DESC|X_PLANT\n"; 
  11.       }
  12.       print;
  13.    }
  14. }
  15. print "all finished";
Jan 22 '07 #6
KevinADC
4,059 Recognized Expert Specialist
is the script itself on the 'c' drive? Try changing to the 'd' drive before openng the files for inserting the newline:

Expand|Select|Wrap|Line Numbers
  1. chdir('d:\\') or die "$!";
or change ino the directory where the files are stored.
Jan 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1668
by: Ali | last post by:
I have the following webpage with a javasctript in it: <html> <head> <title>Custom Objects Test</title> <script language="javascript"> function PrintCard() { line1 = "<hr>\n"; line2 = "<b>Name: </b>" + this.name + "<br>\n"; line3 = "<b>Email: </b>" + this.email + "<br>\n";
2
2217
by: Cle | last post by:
Hi the CGI-Perl experts. I known how to write to a file from within CGI, and I could access Drive A:\ from HTML codes , such as: file://A: My question for your help: PLease show me how to do this from a CGI Perl script: http://www.mysite.com/cgi-bin/cgiwrap/MyName/FileName.pl ?
6
3836
by: rxl124 | last post by:
someone please please help w/ this one. As I been working on this on and off and it just does not want to work. 1 #!/usr/bin/perl -w 2 3 $file = "/home/user1/dothis"; 4 open(FILE, ">$file"); 5 while($line = <FILE>) { 6 if ($line =~ /B/) {print FILE "A"}; 7 if ($line =~ /A/) {print FILE "B"};
8
6253
by: von | last post by:
I am writing data from a Javascript to a text file using a Perl script and it all works pretty well - except ... This: "Here is my data" becomes: "Here%20is%20my%20data" when it gets to the text file.
6
9317
by: Filiz Duman | last post by:
I was just wondering, is it possible to write into the drop down box in order to jump to a specific item. The reason why I am asking is my drop down box has many items and additionally to the scrolling down, it would be good if the user can write into the box in order to get the specific entry the list selected. Is this possible ? Thanks.
1
1726
by: von | last post by:
I am trying to write a single piece of data (that is generated from a Javascript) to a text file on my server via a Perl script. The Javascript is setup so that I can display the required data on my website using the following HTML: <span name='Data1' class='data1'></span> But I need it sent to a text file. :(
4
2111
by: Vladislav | last post by:
My customers have reported strange behaviour of the locally used modules for running the register on stroke patients, specifically, adding new patient to the register. When analising a sample, I have found that an autonumber variable (id) doesn't behave as expected. At some instant after the record with id=180, the next record is coming with id=158. I can't find any reasonable explanation. Could anybody? V.M.
1
1976
by: jonathan184 | last post by:
trying to rename filenames and extensions then add a header in line1 of each file if the header existed in line 1 to go to the next file. but i am getting error explciti errors Here is my code I am using #!/usr/bin/perl user warnings;
21
5807
by: Mick1000 | last post by:
Hi all, I am new to perl and this forum. I am trying to setup a mailing list subscription functionality for customers to receive a periodic newsletter from me. My perl program grabs the html form 'email address' text input but I am then having issues writing this data to a plain text file. Below is my html form followed by my perl script. When i run this on IIS, i dont receive any errors, nor does anything get written to the file. Any help...
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.