简体   繁体   中英

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

Assume file2 content is as below

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".

Basically, I want to search for pattern in file2 and paste the contents in the next line after that pattern.

Note: this "bbb bbb bbb" can be at any line no in 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:

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

If ed is available.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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