bashawksedcattac

Merging of two files with substitution


I need to merge two files, adding the contain of the file 2 directly to the end of the file 1. Notable the last line of the first file consist of the phrase "END". I need to remove it and add there the content of the file 2:

file 1:

string 1
string 2
string N
END

file 2:

string 3
string 4
string M

should give me the file 3

string 1
string 2
string N
string 3
string 4
string M

I've tried simply using

cat file1 file2 >> file3

but it did not substitution of the END in the file2. May I use a special option for this?


Solution

  • Using sed:

    sed -e '${r file2' -e ';$d;}' file1
    
    string 1
    string 2
    string N
    string 3
    string 4
    string M
    

    If you want to use shell variable instead of file names directly:

    sed -i.bak -e "\${r $f2" -e ';$d;}' "$f1"