代码:
#! /usr/bin/perl
open(FILEIN,"<tt.txt")||die"can not open the file: $!";
open(FILEOUT,">tt_out.txt");
@filelist=<FILEIN>; #add the file content to @fileList.
#print $filelist[2],"\n";
foreach(@filelist)
{
@strlist=split(/\s+/,$_);
if($_=~/$strlist[1]/)
{
$strlist_upper=uc $strlist[1];
#print "$`$strlist_upper$'";
print FILEOUT "$`$strlist_upper$'";
}
}
实例:
tt.txt:
// hello heloworld ilove
// world lowcase tp uppercase
// n12ll_ckt_rf low case
run the perl file.
tt_out.txt:
// HELLO heloworld ilove
// WORLD lowcase tp uppercase
// N12LL_CKT_RF low case
呵呵,这是帮一个同事写的,感觉这个perl对我来说有两点:
一点是:
if($_=~/$strlist[1]/)
就是说直接获得的变量直接匹配。
二点是:
print FILEOUT "$`$strlist_upper$'";
$` and $' 可以匹配匹配字前后的内容,这个是我以前不知道的。