Using sed to insert file content

后端 未结 5 741
梦如初夏
梦如初夏 2020-11-30 07:54

I\'m trying to insert a file content before a given pattern

Here is my code:

sed -i \"\" \"/pattern/ {
i\\\\ 
r $scriptPath/adapters/default/permissi         


        
5条回答
  •  失恋的感觉
    2020-11-30 08:43

    I got something like this using awk. Looks ugly but did the trick in my test:

    command:

    cat test.txt | awk '
    /pattern/ {
        line = $0;
        while ((getline < "insert.txt") > 0) {print};
        print line;
        next
    }
    {print}'
    

    test.txt:

    $ cat test.txt
    some stuff
    pattern
    some other stuff
    

    insert.txt:

    $ cat insert.txt
    this is inserted file
    this is inserted file
    

    output:

    some stuff
    this is inserted file
    this is inserted file
    pattern
    some other stuff
    

提交回复
热议问题