0

I have markdown README file that have html markers like this:

<!-- CONTRIBUTORS-START -->
<!-- CONTRIBUTORS-END -->

and I want to replace the text inside (it may already have stuff) with new content from a file that have html tags and newline characters (it's markdown table with html in cells).

I was trying to replace newlines by 0xFF do sed and after replace it back using this code:

CONTRIBUTORS=`./contributors -u jcubic -r jquery.terminal -m | tr '\n' $'\xFF'`
# contributors script get data from github and display markdown table
# or ERROR if rate limit reached

echo "$CONTRIBUTORS" | grep ERROR > /dev/null || cat README.in | tr '\n' $'\xFF' | \
    sed -e "s%\(<!-- CONTRIBUTORS-START -->\).*\(<!-- CONTRIBUTORS-END -->\)%\1\n${CONTRIBUTORS}\2%" #| tr $'\xFF' '\n'

but this don't work, how can I replace text inside markers with CONTRIBUTORS content?

It was kind of working when I use tr '\n' '\xFF' but it was replacing newline by x.

I found this article Sed: Mutli-Line Replacement Between Two Patterns and try it using:

sed -n "/CONTRIBUTORS-START/{p;:a;N;/CONTRIBUTORS-END/!ba;s%.*\n%${CONTRIBUTORS}\n%};p"

(I've replaced / by % in s command) but got error:

sed: -e expression #1, char 1630: unterminated `s' command
0

I've use this hack, I've simply converted new lines to smiley Unicode character "☺", while doing sed replacement, since newlines was causing problems.

CONTRIBUTORS=`./contributors -u jcubic -r jquery.terminal -m $@  | tr '\n' '☺'`

if echo "$CONTRIBUTORS" | grep ERROR > /dev/null; then
    echo "$CONTRIBUTORS" | tr '☺' '\n'
else
    cat README.in | sed -n "/CONTRIBUTORS-START/{p;:a;N;/CONTRIBUTORS-END/!ba;s%.*\n%${CONTRIBUTORS}%};p" | tr '☺' '\n' > README.tmp
    cp README.tmp README.in
    rm README.tmp
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.