Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > add a 3 lines of text in a file before "package" in file

Reply
Thread Tools

add a 3 lines of text in a file before "package" in file

 
 
mike
Guest
Posts: n/a
 
      09-08-2009
Hi,

I am using perl and need to add some text lines before the word
"package" in the file. I have read about Tie::File
and I am trying to use it as below. But I am unable to see how I can
match the word "package" and add
/* ---- Insert text here ---- */
on the line before "package".

Any ideas?

//mike

sub modify {
my @line_array;
my $line;
my ($file) = @_;
print "Modifying file, $file\n";
tie @line_array, 'Tie::File', $file or die "Can not tie file:$!";
for $line(@line_array) {
//
}
untie @line_array;

}
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-08-2009
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

BM> Quoth mike <(E-Mail Removed)>:
>>
>> I am using perl and need to add some text lines before the word
>> "package" in the file. I have read about Tie::File
>> and I am trying to use it as below. But I am unable to see how I can
>> match the word "package" and add
>> /* ---- Insert text here ---- */
>> on the line before "package".


BM> Is the file likely to be large (which probably means several hundred MB,
BM> nowadays)? If not, it would be easier to use File::Slurp and do a simple
BM> s/// on the whole file before writing it out again.


yes, slurping is fine for many megabytes today which is something people
have to learn. text files have not grown like ram sizes have (other than
biogen and similar stuff). slurping this is easy and fast:

(untested)

use File::Slurp ;

my $text = read_file( $file ) ;
$text =~ s{(^.*package.*$)}
{/* ---- Insert text here ---- */\n$1}m ;
write_file( $file, $text ) ;

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-08-2009
mike wrote:
>
> I am using perl and need to add some text lines before the word
> "package" in the file.


$ echo "one
two
three
four package
five
six" | perl -pe'/package/&&print "new text 1\nnew text 2\nnew text 2\n"'
one
two
three
new text 1
new text 2
new text 2
four package
five
six



John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      09-09-2009
Ben Morrow wrote:
> Quoth Tad J McClellan <(E-Mail Removed)>:
>> mike <(E-Mail Removed)> wrote:
>>> sub modify {
>>> my @line_array;
>>> my $line;
>>> my ($file) = @_;
>>> print "Modifying file, $file\n";
>>> tie @line_array, 'Tie::File', $file or die "Can not tie file:$!";

>>
>> foreach my $i ( 0 .. $#line_array) {

>
> This will read the entire file to count the lines before it starts,
> removing any benefit of using Tie::File.


That is far from the only benefit of Tie::File. I don't think it is
even one of the benefits of Tie::File to start with. Not that I think
that Tie::File has all that many benefits to start with. One of the
benefits it does have is letting you write into the middle of a file in
a way that syntactically and logically easy, though quite inefficient.
This benefit is maintained, and ISTM is the whole point.

Xho
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add lines to the beginning of a text file? dean Python 3 04-04-2009 04:31 AM
How to add lines to the beginning of a text file? dean Python 0 04-03-2009 11:16 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments