Bash: Inserting one file's content into another file after the pattern

后端 未结 5 1497
一向
一向 2020-12-30 07:16

I\'m trying to write a bash script, which will do the following:

  1. reads the content from the first file (as a first argument)
  2. reads the content from th
5条回答
  •  攒了一身酷
    2020-12-30 08:00

    sed can do that without loops. Use its r command:

    sed -e '/pattern/rFILE1' FILE2
    

    Test session:

    $ cd -- "$(mktemp -d)" 
    $ printf '%s\n' 'nuts' 'bolts' > first_file.txt
    $ printf '%s\n' 'foo' 'bar' 'baz' > second_file.txt
    $ sed -e '/bar/r./first_file.txt' second_file.txt
    foo
    bar
    nuts
    bolts
    baz
    

提交回复
热议问题