Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding header and footer to multiple files.

Status
Not open for further replies.

lkrb01

Technical User
Joined
Aug 27, 2007
Messages
2
Location
ES
Hi,
First of all, I´m a total newbie to perl, but have make some progress the latest week.
What I have done so far is a script that take a big textfile and change values in it.

The it cut up the textfile to multiple files to have files that not exceed 30000 lines. But my problem is that I also want to add a header and footer to each file. The header and footer include multiple lines.

This is my code so far:

open IN, "+<infil.txt" or die $!;
@file = <IN>;
seek IN,0,0;
foreach $file (@file){
$file =~ s/3/1/g;
$file =~ s/4/X/g;
$file =~ s/5/2/g;
print IN $file;
}
close IN;
open IN, "<infil.txt" or die $!;
$line_num=0; $file_num=0;
open OUT, ">>nextfile".$file_num.".str";
while (<IN>) { if ($line_num%30000==0) {
close OUT;
$file_num++;
open OUT, ">nextfile".$file_num.".str";
}
print OUT $_;
$line_num++;}

close OUT;
close IN;

Perhaps there is a way, so I dont have to open the sourcefile twice also?
 
Which file requires Header/Footers, since ur appending the nextfile and using FileHandle IN twice is a bit confusing to me, but if it makes sense to you :)
 
Your subject "Adding header and footer to multiple files." does not seem to relate to your question. See how ths works, test it with a small file first maybe and change 30000 to 10 or something small.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file_num[/blue] = [fuchsia]1[/fuchsia][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] IN, [red]"[/red][purple]infil.txt[/purple][red]"[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black] OUT, [red]'[/red][purple]>>[/purple][red]'[/red], [red]"[/red][purple]nextfile[blue]$file_num[/blue].str[/purple][red]"[/red] or [black][b]die[/b][/black] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red][black][b]my[/b][/black] [blue]$line[/blue] = <IN>[red])[/red] [red]{[/red]
   [olive][b]if[/b][/olive] [red]([/red][blue]$.[/blue] [blue]%[/blue] [fuchsia]30000[/fuchsia] == [fuchsia]0[/fuchsia][red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] OUT[red];[/red]
      [black][b]open[/b][/black] OUT, [red]'[/red][purple]>>[/purple][red]'[/red], [red]'[/red][purple]nextfile[/purple][red]'[/red] . ++[blue]$file_num[/blue]. [red]'[/red][purple]str[/purple][red]'[/red] or [black][b]die[/b][/black] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
   [red]}[/red]
   [blue]$line[/blue] =~ [red]s/[/red][purple]3[/purple][red]/[/red][purple]1[/purple][red]/[/red][red]g[/red][red];[/red]
   [blue]$line[/blue] =~ [red]s/[/red][purple]4[/purple][red]/[/red][purple]X[/purple][red]/[/red][red]g[/red][red];[/red]
   [blue]$line[/blue] =~ [red]s/[/red][purple]5[/purple][red]/[/red][purple]2[/purple][red]/[/red][red]g[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUT [blue]$line[/blue][red];[/red]
[red]}[/red]
[black][b]close[/b][/black] OUT[red];[/red]
[black][b]close[/b][/black] IN[red];[/red]

This will not change the original file though.

Note: "$." is the input record line number variable. It keeps track of which input record line number is being processed.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It´s the multiple files that I create that should have header and footer. The good thing is that the header and
footer should be the same in all files.

The infil.txt should be unchanged.

The script didn´t work, it didn´t change characters and the files where not named newfile1.str newfile2.str etc.

It create newfile1.str, but then it create newfile1str newfile2str etc.

As I said, I´m a newbie :-)
 
I forgot the dot before 'str':

Code:
open OUT, '>>', 'nextfile' . ++$file_num. '.str' or die "$!";

besides that it looks like the code I posted should work.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top