Sed: add text if pattern not found

Hello,

I would like to add the line TIMEZONE="CET" if the pattern TIMEZONE is not found between the range <JOB and JOB> :

Example:
Src file:

<!DOCTYPE DEFTABLE SYSTEM "deftable.dtd">
<DEFTABLE >
<JOB
TASKTYPE="Job"
TIMEFROM="0030"
TIMEZONE="CET"
</JOB>
<JOB
TASKTYPE="Job"
TIMEFROM="1130"
</JOB>
</DEFTABLE>

Sed script:

#!/bin/ksh

INPUT=$1

sed '
/<JOB/,/JOB>/{
/TIMEZONE/!{
/TIMEFROM/ a\
     TIMEZONE="CET"
}
}' $INPUT > output.txt

The string TIMEZONE="CET" is always added even if the pattern TIMEZONE is present.

I don't see where is the mistake... Any help will be really appreciated!

Thanks a lot!

With awk:

awk '
/<JOB/ {f=1}
f==1 && /TIMEZONE/ {f=0}
/<\/JOB>/ && f {print "TIMEZONE=\"CET\"";f=0}
{print}' $INPUT > output.txt

Regards

Thanks Franklin!

in fact, my XML file is more complex than my example...
Your awk script is not exactly what I need!

My need:

For each block (starting with <JOB and ending with JOB>), check if pattern TIMEZONE is present, if not, add the text TIMEZONE="CET" after the pattern TIMEFROM if present!

With my sed script, the text is always added even the text TIMEZONE is present! (and I don't want that!)

Post a more complex example of your file.

Regards

I got the following sed code to work:

#!/bin/ksh
 
INPUT=$1
 
sed '
/<JOB/,/JOB>/{
/TIMEFROM/ { 
        n
        /TIMEZONE/ !{
                        i\
        TIMEZONE="CET"
        }
}
}' $INPUT > output.txt

The output is as follows:

<!DOCTYPE DEFTABLE SYSTEM "deftable.dtd">
<DEFTABLE >
<JOB
TASKTYPE="Job"
TIMEFROM="0030"
TIMEZONE="CET"
</JOB>
<JOB
TASKTYPE="Job"
TIMEFROM="1130"
        TIMEZONE="CET"
</JOB>
</DEFTABLE>

Only the indented TIMEZONE was added.

Thanks rwuerth! That works fine...

Best regards