简体   繁体   English

bash 命令在文件 2 中的特定字符串/文本之后将文件内容从文件 1 复制到文件 2

[英]bash command to copy file contents from file1 to file2 after particular string/text in file2

Assume file2 content is as below假设 file2 内容如下

aaa aaa aaa
bbb bbb bbb
ccc ccc ccc

I want to copy the contents from file1 to file2 after the line that has the string "bbb bbb bbb".我想在具有字符串“bbb bbb bbb”的行之后将内容从file1复制到file2。

Basically, I want to search for pattern in file2 and paste the contents in the next line after that pattern.基本上,我想在 file2 中搜索模式并将内容粘贴到该模式之后的下一行。

Note: this "bbb bbb bbb" can be at any line no in file2.注意:这个“bbb bbb bbb”可以在file2中的任何行号。

It is very easy: try:这很容易:尝试:

sed -i '/bbb bbb bbb/r file1' file2

If you want to save an updated file2, ed to the rescue:如果要保存更新的文件 2,请ed

ed -s file2 <<EOF
/^bbb bbb bbb$/r file1
w
EOF

If ed is available.如果ed可用。

printf '%s\n' '/^bbb bbb bbb$/r file1.txt' w | ed -s file2.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM