I have been trying but without much success with capture a word between two words and Get Word Between two underscores among many many others...
I want to find the newline before "##", this "##' is after '## baba' but not right after, there is some text between. They are many "##" in the file, always preceded by \n. See below schema:
Desired output
##
## baba {could also be "foo" or "bar"}
rosa rosa rosam rosae ipsum
{append or replace the '\n' before '\n##' with -> helloworld here}
##
##
Once it is found insert "helloworld" given as an argument to the script
My current script find
awk -i inplace -v foo=$2 -v new=$1'\n\n' 'f&&/^##/{print new; f=0} {print} /^## baba/{f=1}' a.md
I want two things: 1/ to replace baba with argument $2 (variable foo), 2/ to include the \n in ^## to have it one line above.
Thank you very much for any help
Edit : Thanks to Rudic I came up with:
a.sh
sed -re "/## $1/,/^\n\n##/ {s/^## *$/$2\n\n\n&/}" a.md
a.md
##
## baba
rosa rosa rosam rosae ipsum
##
##
command line
cat a.md && echo "---------------" && ./test.sh baba remember140416sewol
But output has 2 flaws, 1/ write for each match, I want only the first match, 2/ does not replace the new line before the other newline:
##
## baba
rosa rosa rosam rosae ipsum
{\n <-extra new line}
remember140416sewol
##
remember140416sewol {<-- extra occurence}
##
sed
. – Kusalananda Apr 17 '20 at 09:442/ to include the \n in ^## to have it one line above. – Antonin GAVREL Apr 17 '20 at 18:47