English 中文(简体)
Bash, Perl or Sed, 在发现短语后插入新行
原标题:Bash, Perl or Sed, Insert on New Line after found phrase

Ok I 认为我需要做以下工作:

查阅<代码>/var/lib/asterisk/bin/retrieve_conf中的这一条码:

$engineinfo = engine_getinfo();

插入以下两行:

$engineinfo[ engine ]="asterisk";
$engineinfo[ version ]="1.6.2.11";

Thanks in advance, Joe

最佳回答

你可以这样做。

sed -ne  /$engineinfo = engine_getinfo();/a $ 
  $engineinfo[ engine ]="asterisk"; $ 
  $engineinfo[ version ]="1.6.2.11"; $ 
  ;p  /var/lib/asterisk/bin/retrieve_conf

添加“代码>-i,以便在你确认其运作后加以修改。

它做了哪些工作?

第一,我们听起来说,要看一线,包括你的扼杀。 届时,我们将执行<条码>a指令,即“附录案文”。

缩略语 指挥

a
line of text
another line
;

Note that the literal newlines are part of this syntax. To make it all one line (and preserve copy-paste ability) in place of literal newlines I used $ which will tell bash or zsh to insert a real newline in place. The quoting necessary to make this work is a little complex: You have to exit single-quotes so that you can have the $ be interpreted by bash, then you have to re-enter a single-quoted string to prevent bash from interpreting the rest of your input.

EDIT: 更新,以对两条线进行对应指挥。

问题回答

http://perldoc.perl.org/Tie/File.html (列入伯尔分发文件):

use Tie::File;
tie my @array,  Tie::File , "/var/lib/asterisk/bin/retrieve_conf" or die $!; 
for (0..$#array) {
    if ($array[$_] =~ /$engineinfo = engine_getinfo();/) {
        splice @array, $_+1, 0, q{$engineinfo[ engine ]="asterisk"; $engineinfo[ version ]="1.6.2.11";};
        last;
    }   
}

就是为了这里的不对称,我们用一刀回答。

awk  { if(/$engineinfo = engine_getinfo();/) print $0"
$engineinfo[   engine   ]="asterisk";
$engineinfo[   version   ]="1.6.2.11"" ; else  print $0 }  in.txt

也可使用<条码><>。

# cf. http://wiki.bash-hackers.org/howto/edit-ed
cat <<- EOF  | ed -s /var/lib/asterisk/bin/retrieve_conf
H
/$engineinfo = engine_getinfo();/a
$engineinfo[ engine ]="asterisk";
$engineinfo[ version ]="1.6.2.11";
.
wq
EOF

A Perl one-liner:

perl -pE  s|($engineinfo) = engine_getinfo();.*K|
${1}[   engine   ]="asterisk";
${1}[   version   ]="1.6.2.11";|  file
sed -i   s/$engineinfo = engine_getinfo();/$engineinfo = engine_getinfo();<CTRL V><CNTRL M>$engineinfo[ engine ]="asterisk"; $engineinfo[ version ]="1.6.2.11";/  /var/lib/asterisk/bin/retrieve_conf




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签