由于/r
代表读取文件,请使用:
sed '/First/r file1.txt' infile.txt
你可以在这里找到一些信息:Reading in a file with the 'r' command。
为就地版本添加-i
(即)。sed -i '/First/r file1.txt' infile.txt
无论字符的大小写如何,要执行此操作,请使用 Use sed with ignore case while add text before some patternI
中建议的标记:
sed 's/first/last/Ig' file
如评论中所述,上述解决方案只是在模式之后打印给定的字符串,而不考虑第二个模式。
为此,我会选择带有标志的 awk:
awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
鉴于这些文件:
$ cat patt_file
This is text to be inserted
$ cat file
Some Text here
First
First
Second
Some Text here
First
Bar
让我们运行命令:
$ awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
Some Text here
First # <--- no line appended here
First
This is text to be inserted # <--- line appended here
Second
Some Text here
First # <--- no line appended here
Bar