Bash shell: Replace a text block by another

Hello,
I'm an starter in Bash scripting. I would like to write a script in Bash shell that replaces a specific text block (a function) by another text block in a file:

for example in my file --> $HOME/myFile.js
replacing following function between other functions in the file:

function ABC()
{
  blablabla;
  blablabla;
}

by:

function ABC()
{
 another blablabla;
 another blablabla;
 another blablabla;
}

without doing any change on other text blocks (functions) in the file. Thanks in advance.

You need to change this permanent in a one time event?
Or is it some that changes often?
If so, you can use a if then and select function on condition.

I guess it's not that easy:

$ awk '{gsub("blablabla","another blabla")}1' file

We need some more info: will the new text block be supplied in another file? Is the text block to be replaced the entire function block or just parts thereof?

in fact, I need that in a script that automates installation & configuration of a product.
since there in a bug in one of JavaScript files of the product, the content of one of fuctions should be replaced by something else.
I should install this product several times per month; therefore I'm preparing a script that does all necessary post-installation tasks with minmum manual actions.

---------- Post updated at 07:56 AM ---------- Previous update was at 07:52 AM ----------

new text block is supplied in another file; and will be replace the entire function ABC block.

I'm sorry I can't test right now, but try sth like:

awk    'FNR==NR{Ar[++x]=$0;next}
        $0~Ar[1]{while ($0 != "}") getline; for (i=1;i<=x,i++) print Ar}
        1
       ' rep_block orig_file

The function ABC... line in rep_block must be identical to the one in orig_file.
The proposal reads the entire replacement block into array Ar ; then, tests every line read from orig_file against the first (line of rep_block =) element of Ar . If found, read and discard all the lines until the final } (may need to be escaped) is found; then, print out the Ar holding the replacement text.

Hi.

A perl version for the specifics in this thread:

#!/usr/bin/env bash

# @(#) s1	Demonstrate structured text block replacement.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

FILE=data1
pl " Input data file $FILE:"
cat $FILE

REP=data2
pl " Replacement text file $REP:"

pl " perl code:"
cat p1

pl " Results:"
./p1 $FILE

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 Input data file data1:
any code in the file
function xyz()
{
}
function ABC()
{
  blablabla;
  blablabla;
}
function XYZ()
{
}
any other code in the file

-----
 Replacement text file data2:

-----
 perl code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate structured block replacement.

use warnings;
use strict;

my ( $f, $code, $insert );
open( $f, "<", "data2" ) || die " Cannot open data2\n";
{

  # Best practices, p213 for a file.
  $code = do { local $/; <$f> };
}
close $f;

$insert = 0;
while (<>) {
  if ( /function ABC/ .. /^}/ ) {
    $insert = 1;
    next;
  }
  if ($insert) {
    $insert = 0;
    print $code;
  }

  print;
}

exit(0);

-----
 Results:
any code in the file
function xyz()
{
}
(new) function ABC()
{
 another blablabla;
 another blablabla;
 another blablabla;
}
function XYZ()
{
}
any other code in the file

Best wishes ... cheers, drl

After some fiddling around with above proposal, this version will do the job:

$ awk  'FNR==NR{Ar[++x]=$0;next}
        $0==Ar[1] { while ($0 != "}") getline; getline; for (i=1;i<=x;i++) print Ar}
        1
       ' file1 file
function XYZ()
{
  blablabla;
  blablabla;
}
function ABC()
{
 another blablabla;
 another blablabla;
 another blablabla;
}
function ABC1()
{
  blablabla;
  blablabla;
}

As awk appears to be a bit curt on the empty parentheses pair for a regex, we need to test for the exact identity in order not to lose the pair to distinguish that function from those with similar names. The second getline is needed to consume the last single } .