I fought with it for so long but I am now completely out of ideas. Maybe someone here will be able to help me. Here is what I want to achieve:
file_1.txt
:
# Some comment
some_variable="test"
some other things
Marker
More things!@#$%^
file_2.txt
:
# Marker
# Some other comment
other_variable_1="test"
Some totally other comment
other_variable_2="test"
I want to insert file_2.txt
into file_1.txt
in place of # Marker
and later I want to reverse this process.
Final file file_1.txt
:
# Some comment
some_variable="test"
some other things
Marker
Some other comment
other_variable_1="test"
Some totally other comment
other_variable_2="test"
More things!@#$%^
Problem is, both files are multiline and contains various special characters. I would also like to have both of those files in variables.
I tried various things, sed, perl and awk. Nothing worked for me. This is my closest attempt I think:
perl -pi -e 'chomp if eof' file_2.txt
marker_var="# Marker"
file_2_var=$(tr '\n' '\f' <file_2.txt)
sed -e "s|$marker_var|$file_2_var| tr '\f' '\n'" file_1.txt
I say closest because it it still not working. I tried to combine various answers from stackexchange but It throws error about not properly ended s
. I suspected that it is because of final \n
new line in file so I tried to delete it with perl command but it didn't work.
Can someone please help me?
File2.txt
that you plan on tracking? – jubilatious1 Oct 12 '22 at 04:15I would also like to have both of those files in variables.
- that's usually a bad idea that means you are pursuing the wrong solution to some problem. – Ed Morton Oct 12 '22 at 13:51later I want to reverse this process
- that would mean removing the contents offile_2.txt
from the output of running the merge command and it's not at all clear how you could do that robustly. Do you actually just meanlater I want to reverse the order of the input files and do the same thing
? – Ed Morton Oct 12 '22 at 14:01