Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
linux在字符串匹配之前插入文件内容_Linux_Awk_Sed - Fatal编程技术网

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

linux在字符串匹配之前插入文件内容,linux,awk,sed,Linux,Awk,Sed,嗨,我有一些问题 我有一份原始文件: $cat original.txt User has access to the system $cat toAdd.txt Anna 以及第二个文件,其中包含我要添加到原始文件中的内容: $cat original.txt User has access to the system $cat toAdd.txt Anna 结果如下: $cat original.txt User Anna has access to the system 我尝试了

嗨,我有一些问题

我有一份原始文件:

$cat original.txt
User has access to the system
$cat toAdd.txt
Anna
以及第二个文件,其中包含我要添加到原始文件中的内容:

$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

$awk'FNR==NR{var=$0;next}{for(i=1;iHi,谢谢!不过这只是一个简单的示例。我更喜欢一个解决方案,在该解决方案中,我可以传递要匹配的模式,而不仅仅是基于其在字符串上的位置。在这个示例中,“has”将是要匹配的模式,然后在其前面插入文件中的文本。哦,您尝试了原始问题中的答案吗?是的,我尝试了,即sed'/has/r toAdd.txt'original.txt,但它没有work@MrTeleBird试试我的更新..OMG!!太好了!!这很有效!!非常感谢!!建议的解决方案在这种情况下不起作用。解释为什么suggested解决方案不起作用!