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?
CodePudding user response:
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"
CodePudding user response:
An approach with awk. It !only! skips ENDs occurring on the last line of file1. It recognizes the last line of file1 by using a helper variable set.
Test with multiple ENDs in a file
% awk 'set==""&&NR!=FNR&&last=="END"{last="";set=1}
last!=""{print last}
{last=$0} END{print}' file file
string 1
string 2
END
string N
string 1
string 2
END
string N
END
Using file1 and file2
% awk 'set==""&&NR!=FNR&&last=="END"{last="";set=1}
last!=""{print last}
{last=$0} END{print}' file1 file2
string 1
string 2
string N
string 3
string 4
string M
Data
% cat file
string 1
string 2
END
string N
END
% cat file1
string 1
string 2
string N
END
% cat file2
string 3
string 4
string M
CodePudding user response:
Assumptions:
file1just has the one entry labeledEND- the
ENDentry is at the end offile1
Small modification to OP's current cat idea using process substitution to strip the END from file1:
$ cat <(grep -v '^END$' file1) file2 > file3
$ cat file3
string 1
string 2
string N
string 3
string 4
string M
A variation on Andre's awk solution whereby we print everything we see except the file1/END entry:
$ awk 'FNR==NR && /^END$/ {next} 1' file1 file2 > file3
$ cat file3
string 1
string 2
string N
string 3
string 4
string M
CodePudding user response:
This might work for you (GNU head & cat):
head -n-1 file1 | cat - file2 > file3
Read all but the last line of file1 and concatenate it with file2.
An alternative:
sed -i -e '$f file2' -e '$d' file1
This will append file2 to the end of file1 less the last line of file1 and then replace file1 with the result.
