Replace content that have newlines using sed

jcubic

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
jcubic

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Replace the placeholder - the tilde ~ with the actual content using regular expression and sed

分類Dev

Inline search and replace using sed

分類Dev

Replace div content using jQuery

分類Dev

How to replace values in a file using sed

分類Dev

Replace string variable with string variable using Sed

分類Dev

Using Sed to replace parenthese (and contents) with a period

分類Dev

How to replace a xml file line using sed

分類Dev

using Sed to find and replace a string inside a file

分類Dev

Bash while loop search and replace using sed

分類Dev

Replace a double backslash followed by quote (\\') using sed?

分類Dev

How to replace value with square brackets using sed?

分類Dev

Replace anchor link with specific content using regex

分類Dev

How can I replace a newline (\n) using sed?

分類Dev

How to do replace using sed only in one section of file

分類Dev

How to find & replace pattern WITHIN specific pattern using sed

分類Dev

Find and replace a character in xml file using sed command is not working

分類Dev

How to replace one line with two lines using sed in csh on BSD?

分類Dev

How to print the body content of a html page using sed

分類Dev

How replace Fields in Word document with their content using VBA

分類Dev

Edit web.config using get-content and replace in powershell

分類Dev

Sed replace regex with regex

分類Dev

Replace text in file with SED

分類Dev

sed command to replace a line

分類Dev

How can I replace a filename with .ear in a file-list using sed?

分類Dev

sed - Search and Replace Multiple Line

分類Dev

Sed replace specific line in file

分類Dev

Sed replace specific line in file

分類Dev

sed won't replace text

分類Dev

Sed replace after specific pattern

Related 関連記事

  1. 1

    Replace the placeholder - the tilde ~ with the actual content using regular expression and sed

  2. 2

    Inline search and replace using sed

  3. 3

    Replace div content using jQuery

  4. 4

    How to replace values in a file using sed

  5. 5

    Replace string variable with string variable using Sed

  6. 6

    Using Sed to replace parenthese (and contents) with a period

  7. 7

    How to replace a xml file line using sed

  8. 8

    using Sed to find and replace a string inside a file

  9. 9

    Bash while loop search and replace using sed

  10. 10

    Replace a double backslash followed by quote (\\') using sed?

  11. 11

    How to replace value with square brackets using sed?

  12. 12

    Replace anchor link with specific content using regex

  13. 13

    How can I replace a newline (\n) using sed?

  14. 14

    How to do replace using sed only in one section of file

  15. 15

    How to find & replace pattern WITHIN specific pattern using sed

  16. 16

    Find and replace a character in xml file using sed command is not working

  17. 17

    How to replace one line with two lines using sed in csh on BSD?

  18. 18

    How to print the body content of a html page using sed

  19. 19

    How replace Fields in Word document with their content using VBA

  20. 20

    Edit web.config using get-content and replace in powershell

  21. 21

    Sed replace regex with regex

  22. 22

    Replace text in file with SED

  23. 23

    sed command to replace a line

  24. 24

    How can I replace a filename with .ear in a file-list using sed?

  25. 25

    sed - Search and Replace Multiple Line

  26. 26

    Sed replace specific line in file

  27. 27

    Sed replace specific line in file

  28. 28

    sed won't replace text

  29. 29

    Sed replace after specific pattern

ホットタグ

アーカイブ