Search a character and replace it with multiple lines

This is for AIX 6.1, I've a flat file and the format is like this

DECLARE
    some statements;

BEGIN

    some statements;

END;

I've to search BEGIN and replace it with the following 4 lines

BEGIN
For x in 1..1
LOOP
BEGIN

Similarly I've to search END and replace it with the following text in 6 lines. I cannot combine lines and I need to put semi colon at the end as mentioned below in certain lines.

EXCEPTION 
	when DUP_VAL_ON_INDEX then
		continue;
END;
END LOOP;
END;

Can you please help?

Thanks

Mukul

Hello Mukul Sharma,

Welcome to forum, please use code tags while using putting commands and codes in post as per forum rules. Now for your post, could you please let us know the input and expected output, so that we may help you in same. Kindly make sure you are using code tags while putting commands and codes in post.

Thanks,
R. Singh

Nothing AIX specific, moving.

This is the output from shell script

sharmam@cysvigdcdbora10:/home/sharmam>./repl.ksh
$BEGIN
$END;

This is the flat file to be converted.

sharmam@cysvigdcdbora10:/home/sharmam>cat proc.txt

DECLARE
    null_value CHAR(1) := NULL;
    statement1 CHAR(63);
    statement2 CHAR(56);

BEGIN
    statement1 := 'INSERT INTO rateheader VALUES (:0, :1, :2, :3, :4, :5, :6)';
END;
/

This is the shell script

sharmam@cysvigdcdbora10:/home/sharmam>cat repl.ksh

BEGIN="
BEGIN\
FOR X in 1..1\
LOOP\
        BEGIN\
"

END="
EXCEPTION
        WHEN OTHERS THEN
                CONTINUE;
END;
END LOOP;
"
sed -n 's/BEGIN/$BEGIN/p' proc.txt
sed -n 's/END/$END/p' proc.txt

The expected output is

DECLARE
null_value CHAR(1) := NULL;
statement1 CHAR(63);
statement2 CHAR(56);
BEGIN
FOR X in 1..1
LOOP
BEGIN
statement1 := 'INSERT INTO rateheader VALUES (:0, :1, :2, :3, :4, :5, :6)';
END="
EXCEPTION
WHEN OTHERS THEN
CONTINUE;
END;
END LOOP;
/

Can you please suggest?

Thanks

Following is the fixed code, based on your own approach with sed, which did not work, I guess.
(I cannot guarantee it will work with AIX' version of sed, but it's worth to try, let me know)

BEGIN="\\
BEGIN\\
FOR X in 1..1\\
LOOP\\
        BEGIN\\
"

END="\\
EXCEPTION\\
        WHEN OTHERS THEN\\
                CONTINUE;\\
END;\\
END LOOP;\\
"
sed "s/BEGIN/$BEGIN/;s/END/$END/" proc.txt

---------- Post updated at 10:44 PM ---------- Previous update was at 10:40 PM ----------

Following is another approach, based on awk (note how no wild escaping is required):

$ cat ins1
BEGIN
For x in 1..1
LOOP
BEGIN
$ 
$ cat ins2
EXCEPTION 
    when DUP_VAL_ON_INDEX then
        continue;
END;
END LOOP;
END;
$
awk -v i1="`<ins1`" -v i2="`<ins2`" '
    !/BEGIN/ && !/END/ {print}
    /BEGIN/ {print i1}
    /END/ {print i2}
' file-to-modify

Thanks all, it worked. I really appreciate your help.

Mukul

---------- Post updated at 11:36 AM ---------- Previous update was at 10:00 AM ----------

Hi All, The above sed command "sed "s/BEGIN/$BEGIN/;s/END/$END/" proc.txt" is throwing a semi colon at the next line after the "END;"

END;
END LOOP;
END;
; <<<<<---- This need to be removed and the / should move up.
/

Can you please help?

Thanks

This will remove the semicolon:

sed "s/BEGIN/$BEGIN/;s/END;/$END/" proc.txt

If you want the slash "/" to move up, then replace this piece

END LOOP;\\
"

with

END LOOP;"

Junior-helper

I'm able to remove the semi colon after END but as you suggested to replace END LOOP;\\ with END LOOP;", the command error out.

END;\\
END LOOP;"
END;\\
"

The code below works as expected.

END;\\
END LOOP;\\
END;"

Thanks a lot for your help.