I’m trying with GNU/sed command to append a line into a file inside a block/section, specifically just under the start of the block/section. As a result it empties the file (maybe leaves a linebreak) and then appends the line in question.
The MAGIC_STRING
variable contains the string delimiting the block contents at both ends.
Here’s a script example:
MAGIC_STRING='#!#%%%===!!'
DNS_REGISTRY='ntomato IN A 222.88.1.2n'
file=/home/anyuser/file.txt #file exists and has initial content
appended_string=$(sed -n -i "0,/^$MAGIC_STRING/!b; //a $DNS_REGISTRY" $file)
echo "APPENDED: $appended_string"
[[ -n "$appended_string" ]] && echo "Modified file"
The example file’s initial contents:
#!#%%%===!!
tomato IN A 222.88.1.2
#!#%%%===!!
I tried escaping MAGIC_STRING
characters and putting “” at the start of “a” command. I expected it to match a single MAGIC_STRING
and then just append the line below the start of the block if any and ignore all other lines.
Also, I expected sed to output to stdout the pattern space after modification, which I tested before… but in this case it simply doesn’t for some reason.