I have a problem that is very similar to those SO thread:
how to replace all lines between two points and subtitute it with some text in sed
On my scenario I have an template file like:
...
some code
...
// BEGIN CODE1
// END CODE1
...
some code
...
// BEGIN CODE2
// END CODE2
...
some code
...
The goal is update as needed the contents between CODE1 or CODE2 keeping those comments for future updates (read the script will be on cron Job updating it daily).
Since I'm on FreeBSD my sed dont have the parameters b or p like Linux does, could someone point me how to archive it?
For CODE1 basicaly will be store update the path for some include files.
Have tried this:
#!/bin/sh
directory="/path/to/list/files"
for file in "$directory"/*.txt
do
if [ -f "$file" ]; then
content=$(sed -n '/\/\/ BEGIN CODE1/{:a;N;/\/\/ END CODE1/!ba;p}' "$file")
sed -i '' '/\/\/ BEGIN/,/\/\/ END/{//!c\
INCLUDE /tmp/test.txt
}' "/root/replace.txt"
fi
done
But got this error:
sed: 1: "/\/\/ BEGIN/{:a;N;/\/\/ ...": unexpected EOF (pending }'s)
Desired output for CODE1 would be:
...
some code
...
// BEGIN CODE1
$INCLUDE "/usr/local/etc/namedb/keys/domain.com/ksk.key"
$INCLUDE "/usr/local/etc/namedb/keys/domain.com/zsk.key"
// END CODE1
...
some code
...
// BEGIN CODE2
// END CODE2
...
some code
...
But it need be dynamic, every time cron job runs will put an new path.
FolderKeys="/usr/local/namedb/keys"
FolderSites="/usr/local/etc/namedb/working"
declare -a Sites=( $(ls /$FolderSites | grep '.db$' | sed 's/.db$//') )
for Site in "${Sites[@]}"
do
ls -la $FolderJail$FolderKeys/$Site/*.key
done
Real usage scenario:
I will have an Cron Job checking the $FolderKeys once time per day, and updating the contents daily between:
// BEGIN DNSSEC CODE
// END DNSSEC CODE
On file /usr/local/etc/namedb/working/domain.com.db
This is why I need keep the comments using sed or another tool for replace only the contents inside between:
// BEGIN DNSSEC CODE
// END DNSSEC CODE
For every domain without change other text from namedb zones.