perl将一段文字的第二个字符串由小写转化为大写,其他不变

代码:

#! /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 $' 可以匹配匹配字前后的内容,这个是我以前不知道的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值