jmituzas
jmituzas

Reputation: 603

Bash, Perl or Sed, Insert on New Line after found phrase

Ok I guess I need something that will do the following:

search for this line of code in /var/lib/asterisk/bin/retrieve_conf:

$engineinfo = engine_getinfo();

insert these two lines immediately following:

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

Thanks in advance, Joe

Upvotes: 3

Views: 2630

Answers (6)

sid_com
sid_com

Reputation: 25117

A Perl one-liner:

perl -pE 's|(\$engineinfo) = engine_getinfo\(\);.*\K|\n${1}['\''engine'\'']="asterisk";\n${1}['\''version'\'']="1.6.2.11";|' file

Upvotes: 1

max
max

Reputation: 11

You may also use ed:

# 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

Upvotes: 1

Scott C Wilson
Scott C Wilson

Reputation: 20036

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

Upvotes: 0

sorpigal
sorpigal

Reputation: 26096

Just for the sake of symmetry here's an answer using awk.

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

Upvotes: 1

sorpigal
sorpigal

Reputation: 26096

You could do it like this

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

Add -i for modification in place once you confirm that it works.

What does it do and how does it work?

First we tell sed to match a line containing your string. On that matched line we then will perform an a command, which is "append text".

The syntax of a sed a command is

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 $'\n' 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 $'\n' 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: Updated to append both lines in one append command.

Upvotes: 7

Eugene Yarmash
Eugene Yarmash

Reputation: 150031

You can use Perl and Tie::File (included in the Perl distribution):

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;
    }   
}

Upvotes: 1

Related Questions