Add text between two patterns in File using sed command

后端 未结 4 1247
既然无缘
既然无缘 2020-12-09 06:34

I want to add Some large code between two patterns:

File1.txt

This is text to be inserted into the File.

i

4条回答
  •  执笔经年
    2020-12-09 07:28

    Since /r stands for reading in a file, use:

    sed '/First/r file1.txt' infile.txt
    

    You can find some info here: Reading in a file with the 'r' command.

    Add -i (that is, sed -i '/First/r file1.txt' infile.txt) for in-place edition.

    To perform this action no matter the case of the characters, use the I mark as suggested in Use sed with ignore case while adding text before some pattern:

    sed 's/first/last/Ig' file
    

    As indicated in comments, the above solution is just printing a given string after a pattern, without taking into consideration the second pattern.

    To do so, I'd go for an awk with a flag:

    awk -v data="$(

    Given these files:

    $ cat patt_file
    This is text to be inserted
    $ cat file
    Some Text here
    First
    First
    Second
    Some Text here
    First
    Bar
    

    Let's run the command:

    $ awk -v data="$(

提交回复
热议问题