Search code examples
bashsedtext-fileslinespaces

insert 4 spaces after adding multiple lines from a text file


I want to add multiple lines after matching a pattern. So from

Pattern:
bla

to

Pattern:
    line1-from-file1
    line2-from-file1
bla

I ran something like this sed '/Pattern/r file1' file2 but it gave:

Pattern:
line1-from-file1
line2-from-file1
bla

Solution

  • This might work for you (GNU sed):

    sed 's/^/    /' file1 | sed '/pattern/r /dev/stdin' file2
    

    Pipe a sed amended file1 into a second invocation of sed matching pattern in file2.

    The ameliorated file1 is presented as /dev/stdin and added to the second sed by way of the r command.