Perl:如何在文件中注释行并在文件中附加提取的行范围? [英] Perl:How to comment a line in a file and append an extracted range of lines in a file ?

查看:129
本文介绍了Perl:如何在文件中注释行并在文件中附加提取的行范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在文件中搜索一行,使用"*"注释该行,最后附加从同一文件中提取的相应行的范围.相应的提取行范围可能出现在该行之前或之后(要注释).

以下是我的文件Test.txt

Hi,

I am trying to search for a line in a file, comment that line using a " * " and finally append the range of corresponding lines extracted from the same file. The corresponding extracted range of lines maybe present before or after the line (which is to be commented).

Below is my file Test.txt

Data to be ignored
Data to be ignored

PERFORM P5X98A-PGM-INITS. # This line needs to be commented with a *
#Append a copy of the corresponding extracted range of lines here.
Data to be ignored
Data to be ignored

P5X98A-PGM-INITS SECTION.  # Range to be extracted
data to be stored 1        # Range to be extracted
data to be stored 1        # Range to be extracted
P5X98A-PGM-INITS-EXIT. EXIT. # Range to be extracted

P5X98A-ABC-LIMIT SECTION.
Data to be stored 2
Data to be stored 2
P5X98A-ABC-LIMIT-EXIT. EXIT

Data to be ignored
Data to be ignored

PERFORM P5X98A-ABC-LIMIT.  # This line needs to be commented with a *
#Append a copy of the corresponding extracted range of lines here.
Data to be ignored
Data to be ignored



以下是我的代码.



Below is my code.

<pre lang="Perl">
my $j=0;
open FILE1, "+<Test.txt" or die "Cannot open Test.txt!";

while (<FILE1>) {
	
  if (/$secarr[$j]/../$exarr[$j]/) {
   

    next if /$secarr[$j]/ || /$exarr[$j]/;
    
    if(my $dummy =~  s/^$perform[$j]/* $perform[$j]/)
   {
   	print FILE1 $_;
   }	 
     
  }
 $j++;
}



凡@secarr& @exarr包含开始和结束字符串. @perform数组包含以PERFORM语句开头的所有行

预先感谢,
Faez



Where @secarr & @exarr contains the start and end strings. And @perform array contains all the lines starting with PERFORM statements

Thanks in advance,
Faez

推荐答案

j = 0; 打开FILE1,"+< Test.txt"或死于无法打开Test.txt!"; 同时(< FILE1>){ if(/
j=0; open FILE1, "+<Test.txt" or die "Cannot open Test.txt!"; while (<FILE1>) { if (/


secarr


j]/../


这篇关于Perl:如何在文件中注释行并在文件中附加提取的行范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆