473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How to write to line1 of an exisitng file in perl

154 100+
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 2597
KevinADC
4,059 Expert 2GB
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 100+
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 Expert 2GB
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.bac". 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 Expert 2GB
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 100+
When i run the script below i get an error, now i have the script at d:\\Documents and Settings\\peewee2\\Desktop\\load these\\jan18

and the files that need to have the line added is at d:\\Documents and Settings\\peewee2\\Desktop\\load these\\jan18\\backup\\

but when i run the script I got this message:

Can't open int_sap_contractcontact_180107_999082.txt: No such file or directory
at addline1.pl line 8.
Can't open int_sap_contractcontact_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 Expert 2GB
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
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 =...
2
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...
6
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");...
8
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...
6
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...
1
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...
4
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...
1
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...
21
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
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...

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.