#!/bin/bash # Validates input arguments if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi file=$1 replacement_text_file=$2 # Read replacement text replacement=$(<"$replacement_text_file") # Initialize flag to detect when inside the help section inside=0 # Buffer for storing processed content buffer="" # Process the file while IFS= read -r line || [[ -n "$line" ]]; do if [ "$line" = "" ] && [ "$inside" -eq 0 ]; then # Append opening tag and replacement text buffer+="${line%%*}"$'\n'"$replacement"$'\n' inside=1 elif [ "$line" = "" ] && [ "$inside" -eq 1 ]; then # Append closing tag and reset flag buffer+=""$'\n' inside=0 elif [ "$inside" -eq 1 ]; then # Inside help section, do nothing (skip original content) : else # Outside help section, append the line buffer+="$line"$'\n' fi done <"$file" # Write buffer to the file, correctly handling new lines printf "%s" "$buffer" >"$file"