加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:07:28 所属栏目:大数据 来源:网络整理
导读:代码: #! /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) { ??????? @

代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读