1

I have an TARGET.md file, I'm looking for a string and I want to replace it with the content of other md file, I have tried many combinations but it seems like the newline in the files are the ones sed is not liking, I just need to do this using pure bash(it doesn't have to be sed) because this is how the whole script is running:

This works:

    local search="##### Header"
    local replace="##### Header\\
\\
Line 1\\
Line 2\\
Line 3\\
Line 4"
    sed -i '' -e "s/${search}/${replace}/" TARGET.md

But this won't:

file1.md content:

##### Header

Line 1
Line 2
Line 3
Line 4

Script:

    local search="##### Header"
    local replace=$(curl "path/to/file/in/other/place/file1.md")
    sed -i '' -e "s/${search}/${replace}/" TARGET.md

NOTE: I don't have the file1.md in the same place, I'm doing a curl to get the raw content from it, this is why the replace is in a variable.

I'm assuming the concept is possible but my sed syntax is wrong knowing sed can handle newlines out of the box, but not sure what is the proper way to do this.

I've been searching for some days now, any help, tip or guide is appreciated!

5
  • 1
    Can you show sample TARGET.md Commented Jan 27, 2021 at 19:40
  • @anubhava, TARGET.md is very simple: ##### Header - Test content Commented Jan 27, 2021 at 20:11
  • @oguzismail that I don't actually have the file1.md in the same place, I'm doing a curl to get the raw content Commented Jan 27, 2021 at 20:13
  • 1
    I realized that when I saw the answer, just updated the question, thanks for the comment @oguzismail Commented Jan 27, 2021 at 20:32
  • please update the question with the expected result of the sed command; please update the question with more details of your current issue, eg, what does "this won't" mean? did you get an error? did you get the wrong results? after running replace=$(curl...), show us what's in replace, eg, typeset -p replace Commented Jan 27, 2021 at 22:16

2 Answers 2

2

You are using the wrong tool. sed is a line editor at heart. While you can repeatedly append to pattern space in some instances, awk with getline provides a more flexible solution. For example with your file1.md:

##### Header

Line 1
Line 2
Line 3
Line 4

and your TARGET.md as:

##### Unreleased

my dog
has fleas

The to replace "##### Unreleased" with the content of file1.md, you can do:

awk -v replace="file1.md" -v search="##### Unreleased" '
    $0 == search {while (getline line < replace ) { print line }; next }
                 { print }
' TARGET.md

Above you have your replace and search as with sed, but instead of using the line-editor, you use awk to locate the line containing search and the read all lines from replace using getline`. The second rule just prints all other lines as is.

Example Use/Output

In the directory containing each file, you can simply select-copy the above and middle-mouse paste into the terminal to test:

$ awk -v replace="file1.md" -v search="##### Unreleased" '
>     $0 == search {while (getline line < replace ) { print line }; next }
>                  { print }
> ' TARGET.md
##### Header

Line 1
Line 2
Line 3
Line 4

my dog
has fleas

Look things over and let me know if you have further questions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @david-c-rankin, I failed to explain in my question that I don't actually have the file1.md in the same place, I'm doing a curl to get the raw content.
@RudyPalacios If you curl, be aware curl adds DOS line-endings to the file downloaded. In this case that won't matter, awk treats the carriage-return as whitepace when using the default field-separator, but if you make more changes or add other utilities, after you curl, you use dos2unix file1.md to convert the line-endings, or use sed -i 's/\r/$/' file1.md.
0

Taking TARGET.md file from David's answer:

cat TARGET.md

##### Unreleased

my dog
has fleas

You can run sed with r command like this:

search="##### Unreleased"
sed -e "/$search/{r file1.md" -e ';d;}' TARGET.md

##### Header

Line 1
Line 2
Line 3
Line 4

my dog
has fleas

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.