I want to replace some lines in a file with the content of an other file using sed (on MacOs).
Let say the file is the following and I want to replace all lines that contain <iframe src=...0></iframe>
.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Paupertas si malum est, mendicus beatus esse nemo
<iframe src="https://test.com/bar/11d75d1c627c017299e4fe35e" frameborder=0></iframe>
oportunitatis esse beate vivere. Quasi vero, inquit, perpetua oratio rhetorum solum, non etiam philosophorum
<iframe src="https://test.com/bar/9e4e1b69131bf5d718452aca6" frameborder=0></iframe>
vivere. Quasi vero, inquit, perpetua oratio rhetorum solum, non etiam philosophorum
On the command line the following command works well:
$ sed -i '' -e "/$line/r $replacementFile" -e "//d" $originalFile
where
$line
is the line to change
line="\<iframe src=\"https:\/\/test\.com\/bar\/11d75d1c627c017299e4fe35e\" frameborder=0\>\<\/iframe\>"
$replacementFile
points to the file whose content must replace the$line
s, its content is the following:
LOREM IPSUM DOLOR
AMET, CONSECTETUR
ADIPISCING ELIT.
$originalFile
is the path to the file where$line
has to be changed
I have several files to modify and each one contain several lines to change. So I wrote a script where all lines of type <iframe src=...0></iframe>
are found with a regex, and then I apply the command that worked on the command line.
Below is the original script that I wrote:
function replaceTextWithFile() {
# File to modify (in production the name is passed as an argument)
local originalFile="./file.txt"
# Regex to find the line to change
local regex="<iframe src=\"(https:\/\/test.*)\" frameborder"
# Regex on "normal" line does not work neither
#local patternToReplace="^oportu.*"
# The file whose content must replace the line
local replacementFile="./new_content"
exec 4<"$originalFile"
while read line <&4 ; do
if [[ $line =~ $regex ]];then
echo "PATTERN FOUND"
sed -i '' -e '/$line/r $replacementFile' -e '//d' $originalFile
fi
# Following command changes the 9th line successfully
sed -i '' -e "12s/.*/TEST TEST/" $originalFile
done
exec 4<&-
}
replaceTextWithFile
But the file is not changed.
I thought that nothing happened because the lines that I want to change have some particular characters (<
, /
, ..), but the script does not work also on "normal" line.
I also thought that the problem is because the file is opened in read mode with a descriptor file, but I can change the content of the file using sed, for example with the following command:
sed -i '' -e "9s/.*/TEST TEST/" $originalFile
I have tried several syntaxes without success, you can find the script and the different syntaxes that I tried here.
Does anyone have any idea what I'm doing wrong?
There are several questions on Stackoverflow that treats some kind of this problem, but none of them has been helpful.
If my first intent is to replace the lines with sed, any other solution would be appreciated.