Script to replace lines in ksh Script

Hi All,

I am novice to Unix and I need your expert advice for the below task.

There is a KSH script file in which I need to replace few line as per the below expectations. So my file look like as

# Host Setup Command:
Line 1
Line 2
Line 3
Line 4
Line Any
_AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$

So in between of # Host Setup Command: and _AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$ there can be N number of lines and I need to replace those lines as per below format

# Host Setup Command:
Syntax 1
Syntax 2
Syntax 3
_AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$

Please let me know if you experts can help me here.

Thanks in Advance.

Rupali

Is this homework/classwork?

This is the solution I need to implement in Production at one shot.

not sure about the class/home work.

How is Syntax 1 etc. supplied? In a file? Calculated? A constant pattern?

Syntax 1 is a simple text I need to replace with the particular line.

Try:

sed s,"Syntax 1","Particular Line",g "$WORKFILE"

When it looks as you want it to, add -i before the workfile.

hth

The following works with without external programs like sed.
It deletes the block between start marker and end marker, and where it's at the end marker puts a replacement text.
Each iteration of the while loop reads a line from "./file" into the variable $line.
The variable $block is non-null if inside the block.
The output in the while loop is redirected to a new file, that is finally copied back to "./file".

block=""
f="./file"
while IFS= read -r line
do
  if [ -n "$block" ] && [ "$line" = '_AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$' ]
  then
    block=""
    printf "%s" "\
Syntax 1
Syntax 2
Syntax 3
"
  fi
  if [ -z "$block" ]
  then
    printf "%s\n" "$line"
    [ "$line" = '# Host Setup Command:' ] && block=1
  fi
done < "$f" > "$f.new"
[ -s "$f.new" ] &&
cp "$f.new" "$f" &&
rm "$f.new"