On Fri, 8 Mar 2002 14:36:32 +0000 (GMT), [EMAIL PROTECTED] (Zaka rias) wrote:
>i have text file like this : ->
>---------------------
>my name is foo
>foo is my name
>so im foo
>----------------------
>my question are :
>1. I want insert words "foo is me" between line 1 and
>line 2 (between "my name is foo" and "foo is my
>name"), how to do it ?
>
>2. i want append a word "and im fool" after line 1, so
>line 1 becoming "my name is foo and im fool" (still in
>1 line), how to do it?
I'm not sure what you wanted to do with foo, usually
when you use it like that you mean it to be a variable.
So here is a literal way of doing it (with foo = foo)
##########################################################
#!/usr/bin/perl
use warnings;
use strict;
open(FH,"<foo.txt");#
my @in = <FH>; #reads file in to array, 1 line per array element
close FH; #
chomp $in[0]; #remove newline from 1rst line so it can be added to
open(FH,"+>foo.txt"); #opens file for rewrite
print FH $in[0].' and im fool',"\n";
print FH "foo is me\n";
print FH $in[1],$in[2];
close FH;
##########################################################
and here is a way if foo is a name which changes
########################################################
#!/usr/bin/perl
use warnings;
use strict;
open(FH,"<foo.txt");#
my @in = <FH>; #reads file in to array, 1 line per array element
close FH; #
chomp $in[0];
my ($name) = $in[0] =~ m/my name is(.*)/;
$name =~ s/^\s+//; #only gets leading whitespace
open(FH,"+>foo.txt"); #opens file for rewrite
print FH $in[0].' and im fool',"\n";
print FH "$name is me\n";
foreach (@in){$_ =~ s/foo/$name/}
print FH $in[1],$in[2];
close FH;
######################################################3
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]