Hello,
I am very new to Unix and using sed so I'm struggling a little bit with this command and was hoping someone could help me out.
I want to find a nested pattern in a file and replace some text in that file with text from another file.
For example, in file one I have something like this:
def bikeModels(myBike = getBike())
if myBike == "Huffy":
return ["H1","H2","H3"]
elif myBike == "Mongoose":
return ["M1","M2","M3"]
***Blank line separates these two sections***
def bikeColors(myBike = getBike())
if myBike == "Huffy":
return ["Black","Blue"]
elif myBike == "Mongoose":
return ["Red","White"]
In my file two all I have is a new set of Huffy models:
return ["H4","H5","H6"]
which I want to put into file one, replacing what was originally being returned if myBike == "Huffy" under bikeModels to get a file that looks like this:
def bikeModels(myBike = getBike())
if myBike == "Huffy":
return ["H4","H5","H6"]
elif myBike == "Mongoose":
return ["M1","M2","M3"]
def bikeColors(myBike = getBike())
if myBike == "Huffy":
return ["Black","Blue"]
elif myBike == "Mongoose":
return ["Red","White"]
I can use sed to find the chunk of the file I want with a command like:
sed -n '/bikeModels/,/^$/ {myBike == "Huffy"/,/]/p}; file1
but I have no idea how to replace that text with the text from file two.
I did see I could use:
sed -e '/regex/{r insert.file' -e 'd;}' source
but, again, I have no idea how to combine everything I want to do and have it work.
Thanks to anyone that can help. Let me know if I need to go into more detail or explain anything better.