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
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"