3 replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
source | link

Possible Duplicate:
Insert a multiline string into another stringInsert a multiline string into another string

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.

Possible Duplicate:
Insert a multiline string into another string

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.

Possible Duplicate:
Insert a multiline string into another string

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.

2 insert duplicate link
source | link

Possible Duplicate:
Insert a multiline string into another string

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.

Possible Duplicate:
Insert a multiline string into another string

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.

    Post Closed as "exact duplicate" by Michael Mrozek of
1
source | link

Replace matches with multiline string using sed

I have a file called foo.txt containing:

foo
bar
baz

I want to replace every occurence of bar with a multiline string, stored in the variable $bar:

1
2
3

Resulting in:

foo
1
2
3
baz

I can do this in awk using:

echo "$bar" | awk '{
        if(FILENAME == "-")
        {
            if(i != "")
            {
                i = i"\n"
            }
            i = i$0
        }
        else
        {
            if($0 == "bar")
            {
                printf i
            }
            else
            {
                print
            }
        }
    }'

However trying to do the same in sed using sed 's/bar/'"$bar"'/' foobar.txt gives the error:

sed: 1: "s/bar/1
2
3/": unescaped newline inside substitute pattern

Can this be fixed?

If there is an even easier way of doing this using another standard tool, I would like to know that too.