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.
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.
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.
% ./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
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 } .