473,406 Members | 2,345 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,406 software developers and data experts.

trying to rename filenames and extensions then add a header in line1 of each file

154 100+
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
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. user warnings;
  4. use strict;
  5. use Tie::File;
  6.  
  7.  
  8. opendir(DIR, ".") or die $!;
  9.  
  10. my @files = readdir(DIR); # reads everything in the dir
  11. close(DIR);    #
  12.  
  13. $i = 1;    #declared variable
  14. # While loop implemented with the array
  15. while (@files) {
  16.  
  17.    @row = split(/\./,$_); # will return each line listed in the read  filename. ext - or whatever is listed
  18.  
  19.    $name = $row[0];    # will read everything on the left of the . meaning will read the file name alone
  20.    chomp($name);
  21.  
  22.    $new_name = $name.$i.'.txt' # The new name variable is declared so that the the filename left of the. will include the $i ands concatnate then end in .txt
  23.    rename($_, $new_name);    # will return filename, ==> which will rename to the newname in the variable above.
  24.  
  25.    $i++;
  26.  
  27. ################ Adding line 1 - Header information for each file that does not have it###############
  28.  
  29.  
  30. tie @files, 'Tie::File', '$new_name' || die "Can't open: $!\n";
  31. $numrows = @files;
  32.  
  33. $insertpoint = $numrows - 1;
  34. $newrec="PARTY_UID|NAME|ADDR_ADDR|ADDR_ADDR_LINE_2|ADDR_ADDR_LINE_3|ADDR_ADDR_NAME|ADDR_ZIPCODE|ADDR_POBOX|ADDR_CITY|ADDR_PROVINCE|ADDR_COUNTRY|MAIN_PH_NUM|MAIN_FAX_PH_NUM|LANG_ID|X_VAT_NUM|TAX_JURIDICTION_CODE|ACCOUNT_GROUP|COMPLETE_DELIVERIES|INCO_TERMS1|INCO_TERMS2|CUSTOMER_GROUP|CUSTOMER_GROUP3|CUSTOMER_GROUP5|PRICE_LIST_TO_USE|SALES_ORG|BASE_CURCY_CD|PAY_TERM_NAME|CREATED|CREATED_BY|TAX_EXEMPT_FLG|X_PPL_HOLD|X_PPL_HOLD_DATE|X_CREDIT_HOLD
  35. ";
  36. splice @rows, $insertpoint, 0, $newrec;
  37. untie @rows; 
  38.  
  39.  
  40. };
Dec 5 '06 #1
1 1962
. . . add a header in line1 of each file . . . if the header existed in line 1 to go to the next file.
Hello! I'm only going to tackle this first "requirement." I'd approach this without loading the entire file contents into an array, and using splice. Instead, I'd do something like this:

Expand|Select|Wrap|Line Numbers
  1. $line_count = 0;
  2. while (<>) {
  3.     $line_count++;
  4.  
  5.     if ($line_count == 1) {
  6.         # First line. Check if header row exists using fancy Perl RE
  7.  
  8.         if (!/^HEADER$/) {
  9.             print "HEADER\n";
  10.         }
  11.     }
  12.     print;
  13. }
  14.  
As you can see, all the action happens when the program sees the first line. In the first line, if the HEADER row is not present, then print it. If the HEADER row is present, then don't print it.

I copied the above into a small Perl program file, and ran it locally (in Cygwin).

Expand|Select|Wrap|Line Numbers
  1. 21:40:34 575> cat -n /tmp/newfile.txt
  2.      1  HEADER
  3.      2  This
  4.      3  is
  5.      4  a
  6.      5  sample
  7.      6  file.
  8.  
  9. 21:40:39 576> perl addheader.pl </tmp/newfile.txt           
  10. Processed 6 lines.
  11. HEADER
  12. This
  13. is
  14. a
  15. sample
  16. file.
  17.  
  18. 21:41:27 579> cat -n /tmp/oldfile.txt 
  19.      1  This
  20.      2  is
  21.      3  a
  22.      4  sample
  23.      5  file without the header row.
  24.  
  25. 21:41:57 581> perl addheader.pl </tmp/oldfile.txt 
  26. Processed 5 lines.
  27. HEADER
  28. This
  29. is
  30. a
  31. sample
  32. file without the header row.
  33.  
I spend plenty of time in front of huge files (on the order of 500 to 1000 megabytes), and I try to avoid slurping in a large file into an array if I can help it. For this operation (adding a header row), it makes sense tackle it in a "streaming" fashion.
Dec 6 '06 #2

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

Similar topics

9
by: Steve Loft | last post by:
I have a PHP script which generates responses to answers submitted by a form. The responses contain links to images. I'd like to stop users guessing the names of other images and viewing them. I...
3
by: fanbanlo | last post by:
C:\MP3\001.txt -> 0.txt C:\MP3\01. ??? - ????(???).mp3 -> 1.mp3 Traceback (most recent call last): File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in...
0
by: Bruce P. | last post by:
I have a C++ routine that uses the fcloseall() command, then moves immediately into a routine that renames the files just closed. Occasionally, a rename will fail, even though both the "from" and...
3
by: Venkat | last post by:
Hi All, Currently i guess rename and remove are not supported using fstream. How do i rename or remove a file? regards, Venkat
9
by: rodchar | last post by:
hey all, i was just wondering if you name the web.config any name as long as it has the .config? True or False, why. thanks, rodchar
8
by: Merlin | last post by:
Ok.... I feel really dumb on this one, because I had previously figured it out, and now don't have a clue. I'm trying to get the user to input a new filename (via an integer variable) and...
0
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
3
by: erbrose | last post by:
Hey all, Im back and in need of some help again. Once again I have been tasked with fixing a problem in someone else script and am completely stuck. The process of the script works just fine...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.