回复 沙发 的帖子
Try, It works well on my PC. The content is stored in your file which is named "testfile.txt"
#!/usr/local/bin/perl
use warnings;
use strict;
my @TXT = ();
my @user = ();
my $line;
my $name1 = shift(@ARGV);
my $name2 = shift(@ARGV);
my $changed_line;
# `sed -i -e 's!active!inactive!g' testfile.txt`;
&search_name($name1, $name2);
#######################################################
sub search_name {
my($head1, $head2) = @_;
open (FH, "+<testfile.txt") || die ("cannot open the file\n");
@TXT = <FH>;
#print FH "nihao";
#print"wang yang 2: the content is $TXT[0]\n";
foreach $line(@TXT)
{
chomp($line);
open(FH2, ">>tempfile.txt") || die ("cannot open the temp file\n");
if ($line =~ m/$head1/i){
print"find the person $head1 in this file !\n";
print "The content of this line is: $line\n";
$line =~s/active/inactive/g;
# print " wang yang:$line\n";
print"####Go on continue to find other persons########\n\n";
$line .="\n";
print FH2 $line;
}
elsif ($line =~ m/$head2/) {
print"find the person $head2 in this file !\n";
print "The content of this line is: $line\n";
$line =~s/active/inactive/g;
$line .="\n";
print FH2 $line;
}
else {
print"Mismatch\n";
$line .="\n";
print FH2 $line;}
# my $curpos = tell(FH);
# seek(FH, $curpos, 0);
close FH2;
}
close FH;
system("rm -f testfile.txt");
system("mv tempfile.txt textfile.txt");
}