由于
/r代表 读取文件 ,请使用:
sed '/First/r file1.txt' infile.txt
您可以在此处找到一些信息:使用’r’命令读取文件。
为就地版本添加
-i(即
sed -i '/First/r file1.txt' infile.txt)。
要执行此操作,无论字符大小写如何,均应使用在忽略大小写时使用sed中
I建议的标记,同时在某些模式之前添加文本:
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_fileThis is text to be inserted$ cat fileSome Text hereFirstFirstSecondSome Text hereFirstBar
让我们运行命令:
$ awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' fileSome Text hereFirst # <--- no line appended hereFirstThis is text to be inserted # <--- line appended hereSecondSome Text hereFirst # <--- no line appended hereBar


