replace one section in a datafile

Hi:
First, this is not a homework problem. I just need enough of a hint to get this going...
My datafile (dataf.in) is made up of 10 sections. Each section begins with & and with &&
So it looks like this:-------------------------------------
&section1
...etc...
&&

&section2
...etc...
&&

------------------

I need to replace section2 with a new section that I call "section_new" contained
in file (dataf.new). So now, dataf.in looks like this now:
&section1
...etc...
&&

&section_new
...etc...
&&

I am trying to do that with no luck with a shell script or awk.
Is that feasible? or should I be coding it in C?
Any suggestion or feedback is appreciated.

Best,
Paprika :frowning:

(
    awk '
        BEGIN {count=0}
        /^&/ {
            if (substr ($0,1,2) != "&&") ++count
            if (count == 2) exit
        }
        {print}
        ' file1

    cat file2

    awk '
        BEGIN {count=0}
        (count == 2) {print; next}
        /^&&/ {++count}
        ' file1
) > newfile

Not pretty but it should work ...

Thanks, Kemisola. It works almost. The new paragraph is inserted.
However the old one remains. As I include comments in your script,
I should be able to a way to delete the old paragraph. :slight_smile:

Instead of
if (count == 2) exit
should be
if (count == 1) exit

Also I think "substr" is not necessary. "^&&" marks the end of a section.

In my previous posting I meant
/^&&/ {++count; ...
instead of /^&/ { ... substr ...}