linux 在字符串匹配之前插入文件内容

标签 linux awk sed

<分区>

您好,我遇到了一些问题。

我有一个原始文件:

$cat original.txt
User has access to the system

还有第二个文件,其中包含我要添加到原始文件的内容:

$cat toAdd.txt
Anna

结果应该是这样的:

$cat original.txt
User Anna has access to the system

我尝试了几个选项,例如:

sed '/has/e cat toAdd.txt' original.txt

但它不起作用:-(

请帮忙!!

最佳答案

通过awk,

$ awk 'FNR==NR{var=$0; next}{for (i=1;i<=NF;i++){if($i=="has"){$i=var" "$i}}}1' toAdd.txt original.txt
User Anna has access to the system

关于linux 在字符串匹配之前插入文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276044/

相关文章:

sed - 需要解析如下sed命令 : sed -e 's//\' $'\n/g'

c - 命令行参数位于何处?

c - 在中断处理程序中将数据从内核缓冲区移动到用户空间缓冲区

linux - 如何在 vi 编辑器中一次搜索 3 个字符串?

bash - sed-style 用随机字母替换字母,用随机数替换数字

awk - 如何在tshark功能tshark管道后添加管道

linux - 在 bash 脚本中使用 "alternate screen"

linux - 替换 awk 的结果

bash - AWK:根据上一行的值增加字段

templates - Jenkins 管道 : templating a file with variables