Bash 使用命令行linux在单行位置插入文本文件的全部内容

Bash 使用命令行linux在单行位置插入文本文件的全部内容,bash,command-line,sed,str-replace,Bash,Command Line,Sed,Str Replace,我基本上是试图用命令行(linux)中另一个文本文件的全部内容替换一行文本。你知道怎么做吗?这里有一种使用awk awk 'FNR==NR {a[NR]=$0;f++;next} /trigger/ {for (i=1;i<=f;i++) print a[i];next}1' newdata orgfile awk'FNR==NR{a[NR]=$0;f++;next}/trigger/{for(i=1;i你可以试试这个sed sed -e '/trigger/r newfile' -e

我基本上是试图用命令行(linux)中另一个文本文件的全部内容替换一行文本。你知道怎么做吗?

这里有一种使用
awk

awk 'FNR==NR {a[NR]=$0;f++;next} /trigger/ {for (i=1;i<=f;i++) print a[i];next}1'  newdata orgfile

awk'FNR==NR{a[NR]=$0;f++;next}/trigger/{for(i=1;i你可以试试这个
sed

sed -e '/trigger/r newfile' -e '/trigger/d' org_file
这里,

newfile
将有一个内容,当在
org\u file
中找到
trigger
时,将插入一个内容

测试:

$ cat > org_file
line 1
line 2
line 3
trigger
line 6
line 7
$ cat > newfile
line 4
line 5
$ sed -e '/trigger/r newfile' -e '/trigger/d' org_file 
line 1
line 2
line 3
line 4
line 5
line 6
line 7

sat答案的一个变体,带有
sed
,不需要读取
org\u file.txt
两次:

sed '/trigger/{
         r newfile.txt
         d
     }' org_file.txt
或者使用更少的换行符:

sed '/trigger/{r newfile.txt
  d}' file.txt
有一个缺点:它不是一行,因为
r
命令将它后面的所有内容解释为文件名(更多详细信息)。
Peter.O提供了一个变通方法(参见上面的链接):


sed-f是否需要添加数据?如果使用python,解决方案非常优雅:
sed -f <(sed 's/\\n/\n/'<<<'/trigger/{r new_file.txt\nd}') org_file.txt