Perl - 在文件中找到匹配项后插入行 [英] Perl - Insert lines after a match is found in a file

查看:81
本文介绍了Perl - 在文件中找到匹配项后插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 some_1.xyz 中有一个具有以下语法的文件

I have a file with the following syntax in some_1.xyz

module some_1 {
INPUT PINS
OUTPUT PINS
}

并且我想在 line module some_1 {

and I want to insert APPLY DELAYS xx and APPLY LOADS ld after line module some_1 {

以下代码仅适用于一个文件,即,如果我将 some_1.xyz 替换为 *.xyz,则该脚本不起作用.我尝试引入 sleep(xx) 但代码对多个文件不起作用,我无法弄清楚为什么它不起作用.任何指针表示赞赏.谢谢

The following code works fine for just one file i.e., if I replace some_1.xyz to *.xyz then the script doesn't work. I tried introducing sleep(xx) but the code doesn't work for multiple files and I could not figure out why it isn't working. Any pointers is appreciated. Thanks

@modulename_array = `grep "module " some_1.xyz | cut -f 2 -d ' '`;
@line = `grep "module " some_1.xyz`;

chomp(@line);
chomp(@kfarray);

$i = 0;
foreach (@modulename_array) {
  print "Applying delay and load to $_.xyz $line[$i] \n";

  `perl -ni -le 'print; print "\tAPPLY DELAY xx \n \tAPPLY LOADS  ld\n" if/$line[$i]/' $_.xyz`;
  $i++;
  #sleep(3);

}

推荐答案

那么简单的解决方案有什么问题?:

And what's wrong with the easy solution?:

$data=`cat /the/input/file`;
$data=~s/some_1 {\n/some_1 {\nAPPLY DELAYS xx\nAPPLY LOADS ld\n/gm;
print $data;

这篇关于Perl - 在文件中找到匹配项后插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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