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;
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.
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;
/
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;
$