perl 获取文件内容里第一个AAA和最后一个AAA

Perl脚本解析文件示例
本文介绍了一个使用Perl语言编写的脚本示例,该脚本用于从文本文件中筛选包含特定字符串‘AAA’的行,并记录这些行的行号及具体内容。通过此示例展示了Perl处理文本文件的基本方法。
<pre name="code" class="html">[root@wx03 ~]# cat -n aaa
     1	3`13
     2	edqae
     3	daa
     4	313
     5	1313
     6	1
     7	AAAhash1
     8	e31
     9	dad
    10	fad
    11	AAAA
    12	32131
    13	AAA
    14	qe
    15	dad
    16	AAA
    17	fdaaf
    18	fafa
    19	AAAhash2
[root@wx03 ~]# cat q1.pl 
my @arr=();
open (LOG ,"<","aaa");  
      while (<LOG>) { 
      if ($_ =~/AAA/){$num=$.;push (@arr,$num); $hash{$num}=$_;};
};
print $arr[0]."\n";
print $hash{$arr[0]}."\n";
print $arr[-1]."\n";
print $hash{$arr[-1]}."\n";

[root@wx03 ~]# perl q1.pl 
7
AAAhash1

19
AAAhash2


   

转载于:https://www.cnblogs.com/zhaoyangjian724/p/6199670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值